Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit eea1262

Browse files
solves compare version number
1 parent 0d5fdb0 commit eea1262

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | [![Java](assets/java.png)](src/IntersectionOf2LinkedLists.java) [![Python](assets/python.png)](python/intersecction_of_two_linked_lists.py) | |
145145
| 161 | 🔒 [One Edit Distance](https://leetcode.com/problems/one-edit-distance) | | |
146146
| 162 | [Find Peak Element](https://leetcode.com/problems/find-peak-element) | [![Java](assets/java.png)](src/FindPeakElement.java) | |
147-
| 165 | [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers) | | |
147+
| 165 | [Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers) | [![Java](assets/java.png)](src/CompareVersionNumbers.java) | |
148148
| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal) | | |
149149
| 167 | [Two Sum II - Input Array is Sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted) | [![Java](assets/java.png)](src/TwoSumIIInputArrayIsSorted.java) [![Python](assets/python.png)](python/two_sum_ii.py) | |
150150
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | [![Java](assets/java.png)](src/ExcelSheetColumnTitle.java) [![Python](assets/python.png)](python/excel_sheet_column_title.py) | |

src/CompareVersionNumbers.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// https://leetcode.com/problems/compare-version-numbers
2+
// T: O(|v1| + |v2|)
3+
// S: O(v1 + v2)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
public class CompareVersionNumbers {
9+
public static int compareVersion(String version1, String version2) {
10+
final List<Integer> revisions1 = getRevisions(version1);
11+
final List<Integer> revisions2 = getRevisions(version2);
12+
return compareVersion(revisions1, revisions2);
13+
}
14+
15+
public static int compareVersion(List<Integer> version1, List<Integer> version2) {
16+
int length = Math.max(version1.size(), version2.size());
17+
for (int i = 0 ; i < length ; i++){
18+
if (get(version1, i) < get(version2, i)) return -1;
19+
else if (get(version1, i) > get(version2, i)) return 1;
20+
}
21+
return 0;
22+
}
23+
24+
private static int get(List<Integer> list, int index) {
25+
if (index >= list.size()) return 0;
26+
return list.get(index);
27+
}
28+
29+
private static List<Integer> getRevisions(String version) {
30+
final String[] revisions = version.split("\\.");
31+
final List<Integer> result = new ArrayList<>();
32+
for (String revision : revisions) {
33+
result.add(Integer.parseInt(revision));
34+
}
35+
return result;
36+
}
37+
}

src/HelloWorld.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
public class HelloWorld {
22
public static void main(String[] args) {
3-
System.out.println(
4-
MaximumProductSubarray.maxProduct(new int[] {-4, -3, -2})
5-
);
3+
System.out.println(CompareVersionNumbers.compareVersion("0.1", "1.1"));
64
}
75
}

0 commit comments

Comments
 (0)