165. Compare Version Numbers - LeetCode
阿新 • • 發佈:2018-08-28
alt pre desc ever 分享圖片 ems 個數 src lock
Question
165. Compare Version Numbers
Solution
題目大意:
比較版本號大小
思路:
根據逗號將版本號字符串轉成數組,再比較每個數的大小
Java實現:
public int compareVersion(String version1, String version2) { String[] v1Arr = version1.split("\\."); String[] v2Arr = version2.split("\\."); int i = 0, max = Math.max(v1Arr.length, v2Arr.length); while (i < max) { int v1 = v1Arr.length <= i ? 0 : Integer.parseInt(v1Arr[i]); int v2 = v2Arr.length <= i ? 0 : Integer.parseInt(v2Arr[i]); if (v1 > v2) { return 1; } else if (v2 > v1) { return -1; } i++; } return 0; }
165. Compare Version Numbers - LeetCode