classSolution: defcompareVersion(self, version1: str, version2: str) -> int: for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0): x, y = int(v1), int(v2) if x != y: return1if x > y else -1 return0
publicclassSolution { publicintCompareVersion(string version1, string version2) { string[] v1 = version1.Split('.'); string[] v2 = version2.Split('.'); for (int i = 0; i < v1.Length || i < v2.Length; ++i) { int x = 0, y = 0; if (i < v1.Length) { x = int.Parse(v1[i]); } if (i < v2.Length) { y = int.Parse(v2[i]); } if (x > y) { return1; } if (x < y) { return-1; } } return0; } }
funccompareVersion(version1, version2 string)int { v1 := strings.Split(version1, ".") v2 := strings.Split(version2, ".") for i := 0; i < len(v1) || i < len(v2); i++ { x, y := 0, 0 if i < len(v1) { x, _ = strconv.Atoi(v1[i]) } if i < len(v2) { y, _ = strconv.Atoi(v2[i]) } if x > y { return1 } if x < y { return-1 } } return0 }
var compareVersion = function(version1, version2) { const v1 = version1.split('.'); const v2 = version2.split('.'); for (let i = 0; i < v1.length || i < v2.length; ++i) { let x = 0, y = 0; if (i < v1.length) { x = parseInt(v1[i]); } if (i < v2.length) { y = parseInt(v2[i]); } if (x > y) { return1; } if (x < y) { return -1; } } return0; };
classSolution: defcompareVersion(self, version1: str, version2: str) -> int: n, m = len(version1), len(version2) i, j = 0, 0 while i < n or j < m: x = 0 while i < n and version1[i] != '.': x = x * 10 + ord(version1[i]) - ord('0') i += 1 i += 1# 跳过点号 y = 0 while j < m and version2[j] != '.': y = y * 10 + ord(version2[j]) - ord('0') j += 1 j += 1# 跳过点号 if x != y: return1if x > y else -1 return0
classSolution { public: intcompareVersion(string version1, string version2){ int n = version1.length(), m = version2.length(); int i = 0, j = 0; while (i < n || j < m) { int x = 0; for (; i < n && version1[i] != '.'; ++i) { x = x * 10 + version1[i] - '0'; } ++i; // 跳过点号 int y = 0; for (; j < m && version2[j] != '.'; ++j) { y = y * 10 + version2[j] - '0'; } ++j; // 跳过点号 if (x != y) { return x > y ? 1 : -1; } } return0; } };
publicclassSolution { publicintCompareVersion(string version1, string version2) { int n = version1.Length, m = version2.Length; int i = 0, j = 0; while (i < n || j < m) { int x = 0; for (; i < n && version1[i] != '.'; ++i) { x = x * 10 + version1[i] - '0'; } ++i; // 跳过点号 int y = 0; for (; j < m && version2[j] != '.'; ++j) { y = y * 10 + version2[j] - '0'; } ++j; // 跳过点号 if (x != y) { return x > y ? 1 : -1; } } return0; } }
funccompareVersion(version1, version2 string)int { n, m := len(version1), len(version2) i, j := 0, 0 for i < n || j < m { x := 0 for ; i < n && version1[i] != '.'; i++ { x = x*10 + int(version1[i]-'0') } i++ // 跳过点号 y := 0 for ; j < m && version2[j] != '.'; j++ { y = y*10 + int(version2[j]-'0') } j++ // 跳过点号 if x > y { return1 } if x < y { return-1 } } return0 }
var compareVersion = function(version1, version2) { const n = version1.length, m = version2.length; let i = 0, j = 0; while (i < n || j < m) { let x = 0; for (; i < n && version1[i] !== '.'; ++i) { x = x * 10 + version1[i].charCodeAt() - '0'.charCodeAt(); } ++i; // 跳过点号 let y = 0; for (; j < m && version2.charAt(j) !== '.'; ++j) { y = y * 10 + version2[j].charCodeAt() - '0'.charCodeAt(); } ++j; // 跳过点号 if (x !== y) { return x > y ? 1 : -1; } } return0; };