-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path165.compare-version-numbers.java
More file actions
35 lines (29 loc) · 1 KB
/
165.compare-version-numbers.java
File metadata and controls
35 lines (29 loc) · 1 KB
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
/*
* @lc app=leetcode id=165 lang=java
*
* [165] Compare Version Numbers
*/
// @lc code=start
class Solution {
public int compareVersion(String version1, String version2) {
//need to escape the dot if you want to split on a literal dot:
//String extensionRemoved = filename.split("\\.")[0];
//Otherwise you are splitting on the regex ., which means "any character".
String[] ver1Arr = version1.split("\\.");
String[] ver2Arr = version2.split("\\.");
int index = 0;
while (index < Math.max(ver1Arr.length, ver2Arr.length)) {
int ver1Value = index < ver1Arr.length ? Integer.valueOf(ver1Arr[index]) : 0;
int ver2Value = index < ver2Arr.length ? Integer.valueOf(ver2Arr[index]) : 0;
if (ver1Value < ver2Value) {
return -1;
} else if (ver1Value > ver2Value) {
return 1;
} else {
index++;
}
}
return 0;
}
}
// @lc code=end