8000 solve #165 · tinycedar/leetcode-rust@63537e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 63537e5

Browse files
committed
solve #165
1 parent 6fded09 commit 63537e5

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,4 @@ mod n0154_find_minimum_in_rotated_sorted_array_ii;
152152
mod n0155_min_stack;
153153
mod n0162_find_peak_element;
154154
mod n0164_maximum_gap;
155+
mod n0165_compare_version_numbers;

src/n0164_maximum_gap.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub struct Solution {}
3535
想不出来, 一看解析居然是 Radix Sort 或 Bucket Sort, 我就 ??? 了...
3636
3737
最佳算法是 Bucket Sort 吗? (桶大小取 max - min / len 那种), 看时间复杂度好像是这样
38+
39+
但假如整体排布非常稠密, 那么这个聪明的算法也就退化成了桶大小为 1 的桶排序
3840
*/
3941
impl Solution {
4042
pub fn maximum_gap(nums: Vec<i32>) -> i32 {

src/n0165_compare_version_numbers.rs

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* [165] Compare Version Numbers
3+
*
4+
* Compare two version numbers version1 and version2.<br />
5+
* If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0.
6+
*
7+
* You may assume that the version strings are non-empty and contain only digits and the . character.
8+
* The . character does not represent a decimal point and is used to separate number sequences.
9+
* For instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
10+
* You may assume the default revision number for each level of a version number to be 0. For example, version number 3.4 has a revision number of 3 and 4 for its first and second level revision number. Its third and fourth level revision number are both 0.
11+
*
12+
*
13+
*
14+
* Example 1:
15+
*
16+
* Input: version1 = "0.1", version2 = "1.1"
17+
* Output: -1
18+
*
19+
* Example 2:
20+
*
21+
* Input: version1 = "1.0.1", version2 = "1"
22+
* Output: 1
23+
*
24+
* Example 3:
25+
*
26+
* Input: version1 = "7.5.2.4", version2 = "7.5.3"
27+
* Output: -1
28+
*
29+
* Example 4:
30+
*
31+
* Input: version1 = "1.01", version2 = "1.001"
32+
* Output: 0
33+
* Explanation: Ignoring leading zeroes, both “01” and “001" represent the same number “1”
34+
*
35+
* Example 5:
36+
*
37+
* Input: version1 = "1.0", version2 = "1.0.0"
38+
* Output: 0
39+
* Explanation: The first version number does not have a third level revision number, which means its third level revision number is default to "0"
40+
*
41+
*
42+
*
43+
* Note:
44+
* <ol>
45+
* Version strings are composed of numeric strings separated by dots . and this numeric strings may have leading zeroes.
46+
* Version strings do not start or end with dots, and they will not be two consecutive dots.
47+
* </ol>
48+
*/
49+
pub struct Solution {}
50+
51+
// submission codes start here
52+
53+
impl Solution {
54+
pub fn compare_version(version1: String, version2: String) -> i32 {
55+
let v1: Vec<&str> = version1.split('.').collect::<Vec<_>>();
56+
let v2: Vec<&str> = version2.split('.').collect::<Vec<_>>();
57+
let mut i = 0_usize;
58+
while i < v1.len() && i < v2.len() {
59+
let left = v1[i].parse::<u32>().unwrap();
60+
let right = v2[i].parse::<u32>().unwrap();
61+
if left > right {
62+
return 1;
63+
} else if left < right {
64+
return - 1;
65+
}
66+
i += 1;
67+
}
68+
while i < v1.len() {
69+
if v1[i].parse::<u32>().unwrap() > 0 {
70+
return 1;
71+
}
72+
i += 1;
73+
}
74+
while i < v2.len() {
75+
if v2[i].parse::<u32>().unwrap() > 0 {
76+
return -1;
77+
}
78+
i += 1;
79+
}
80+
return 0
81+
}
82+
}
83+
84+
// submission codes end
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use super::*;
89+
90+
#[test]
91+
fn test_165() {
92+
assert_eq!(Solution::compare_version( "0.1".to_owned(), "1.1".to_owned()), -1);
93+
assert_eq!(Solution::compare_version( "1.0.1".to_owned(), "1".to_owned()), 1);
94+
assert_eq!(Solution::compare_version( "7.5.2.4".to_owned(), "7.5.3".to_owned()), -1);
95+
assert_eq!(Solution::compare_version( "1.01".to_owned(), "1.0001".to_owned()), 0);
96+
}
97+
}

0 commit comments

Comments
 (0)
0