-
Notifications
You must be signed in to change notification settings - Fork 0
/
s0165_compare_version_numbers.rs
62 lines (57 loc) · 1.66 KB
/
s0165_compare_version_numbers.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
struct Solution;
//leetcode submit region begin(Prohibit modification and deletion)
use std::cmp::Ordering;
impl Solution {
pub fn compare_version(version1: String, version2: String) -> i32 {
let version1_num: Vec<u32> = version1
.split('.')
.map(|x| x.parse::<u32>().unwrap())
.collect();
let version2_num: Vec<u32> = version2
.split('.')
.map(|x| x.parse::<u32>().unwrap())
.collect();
let len1 = version1_num.len();
let len2 = version2_num.len();
for i in 0..len1.max(len2) {
match version1_num
.get(i)
.unwrap_or(&0)
.cmp(version2_num.get(i).unwrap_or(&0))
{
Ordering::Greater => return 1,
Ordering::Less => return -1,
Ordering::Equal => {}
}
}
0
}
}
//leetcode submit region end(Prohibit modification and deletion)
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test() {
assert_eq!(
Solution::compare_version("1.01".to_string(), "1.001".to_string()),
0
);
assert_eq!(
Solution::compare_version("1.0".to_string(), "1.0.0".to_string()),
0
);
assert_eq!(
Solution::compare_version("0.1".to_string(), "1.1".to_string()),
-1
);
assert_eq!(
Solution::compare_version("1.0.1".to_string(), "1".to_string()),
1
);
assert_eq!(
Solution::compare_version("7.5.2.4".to_string(), "7.5.3".to_string()),
-1
);
}
}