-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path165.比较版本号.java
51 lines (42 loc) · 1.08 KB
/
165.比较版本号.java
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
/*
* @lc app=leetcode.cn id=165 lang=java
*
* [165] 比较版本号
*/
// @lc code=start
class Solution {
public int compareVersion(String version1, String version2) {
String[] nums1 = version1.split("\\.");
String[] nums2 = version2.split("\\.");
int index1 = 0;
int index2 = 0;
while(index1<nums1.length && index2<nums2.length){
int c1 = Integer.valueOf(nums1[index1]);
int c2 = Integer.valueOf(nums2[index2]);
if(c1 > c2){
return 1;
}else if(c1 < c2){
return -1;
}
index1++;
index2++;
}
//1.0.1 1.0
while(index1 < nums1.length){
int c1 = Integer.valueOf(nums1[index1]);
if(c1 > 0){
return 1;
}
index1++;
}
while(index2 < nums2.length){
int c2 = Integer.valueOf(nums2[index2]);
if(c2 > 0){
return -1;
}
index2++;
}
return 0;
}
}
// @lc code=end