Skip to content

Commit eb7fa21

Browse files
committed
add 165
1 parent ca00ad6 commit eb7fa21

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
name = "S0165-compare-version-numbers"
3+
version = "0.1.0"
4+
authors = ["xargin <cao1988228@163.com>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}
4+
5+
/*
6+
* @lc app=leetcode.cn id=165 lang=rust
7+
*
8+
* [165] 比较版本号
9+
*/
10+
impl Solution {
11+
pub fn compare_version(version1: String, version2: String) -> i32 {
12+
let (v1, v2) = (
13+
version1
14+
.split(".")
15+
.map(|x| x.parse::<i32>().unwrap())
16+
.collect::<Vec<_>>(),
17+
version2
18+
.split(".")
19+
.map(|x| x.parse::<i32>().unwrap())
20+
.collect::<Vec<_>>(),
21+
);
22+
for i in 0..(v1.len().max(v2.len())) {
23+
match (v1.get(i), v2.get(i)) {
24+
(Some(v1i), Some(v2i)) => {
25+
if v1i > v2i {
26+
return 1;
27+
}
28+
if v1i < v2i {
29+
return -1;
30+
}
31+
}
32+
(Some(v1i), None) => {
33+
if v1i > &0 {
34+
return 1;
35+
}
36+
}
37+
(None, Some(v2i)) => {
38+
if v2i > &0 {
39+
return -1;
40+
}
41+
}
42+
_ => {}
43+
}
44+
}
45+
46+
0
47+
}
48+
}
49+

0 commit comments

Comments
 (0)