165. Compare Version Numbersjava
題目大意:數組
比較版本號大小code
思路:ip
根據逗號將版本號字符串轉成數組,再比較每一個數的大小leetcode
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; }