diff --git a/solution/0100-0199/0165.Compare Version Numbers/README.md b/solution/0100-0199/0165.Compare Version Numbers/README.md index 31ee59fcf8b39..a39094de87e41 100644 --- a/solution/0100-0199/0165.Compare Version Numbers/README.md +++ b/solution/0100-0199/0165.Compare Version Numbers/README.md @@ -192,21 +192,63 @@ func compareVersion(version1 string, version2 string) int { ```ts function compareVersion(version1: string, version2: string): number { - const v1 = version1.split('.'); - const v2 = version2.split('.'); - for (let i = 0; i < Math.max(v1.length, v2.length); ++i) { - const [n1, n2] = [+v1[i] || 0, +v2[i] || 0]; - if (n1 < n2) { - return -1; + const [m, n] = [version1.length, version2.length]; + let [i, j] = [0, 0]; + while (i < m || j < n) { + let [a, b] = [0, 0]; + while (i < m && version1[i] !== '.') { + a = a * 10 + +version1[i]; + i++; } - if (n1> n2) { - return 1; + while (j < n && version2[j] !== '.') { + b = b * 10 + +version2[j]; + j++; } + if (a !== b) { + return a < b ? -1 : 1; + } + i++; + j++; } return 0; } ``` +#### Rust + +```rust +impl Solution { + pub fn compare_version(version1: String, version2: String) -> i32 { + let (bytes1, bytes2) = (version1.as_bytes(), version2.as_bytes()); + let (m, n) = (bytes1.len(), bytes2.len()); + let (mut i, mut j) = (0, 0); + + while i < m || j < n { + let mut a = 0; + let mut b = 0; + + while i < m && bytes1[i] != b'.' { + a = a * 10 + (bytes1[i] - b'0') as i32; + i += 1; + } + while j < n && bytes2[j] != b'.' { + b = b * 10 + (bytes2[j] - b'0') as i32; + j += 1; + } + + if a != b { + return if a < b { -1 } else { 1 }; + } + + i += 1; + j += 1; + } + + 0 + } +} +``` + #### C# ```cs diff --git a/solution/0100-0199/0165.Compare Version Numbers/README_EN.md b/solution/0100-0199/0165.Compare Version Numbers/README_EN.md index a482097276a3b..adc5b1bcebda9 100644 --- a/solution/0100-0199/0165.Compare Version Numbers/README_EN.md +++ b/solution/0100-0199/0165.Compare Version Numbers/README_EN.md @@ -82,7 +82,13 @@ tags: -### Solution 1 +### Solution 1: Two Pointers + +Traverse both strings simultaneously using two pointers $i$ and $j,ドル which point to the current positions in each string, starting with $i = j = 0$. + +Each time, extract the corresponding revision numbers from both strings, denoted as $a$ and $b$. Compare $a$ and $b$: if $a \lt b,ドル return $-1$; if $a \gt b,ドル return 1ドル$; if $a = b,ドル continue to compare the next pair of revision numbers. + +The time complexity is $O(\max(m, n)),ドル and the space complexity is $O(1),ドル where $m$ and $n$ are the lengths of the two strings. @@ -184,21 +190,63 @@ func compareVersion(version1 string, version2 string) int { ```ts function compareVersion(version1: string, version2: string): number { - const v1 = version1.split('.'); - const v2 = version2.split('.'); - for (let i = 0; i < Math.max(v1.length, v2.length); ++i) { - const [n1, n2] = [+v1[i] || 0, +v2[i] || 0]; - if (n1 < n2) { - return -1; + const [m, n] = [version1.length, version2.length]; + let [i, j] = [0, 0]; + while (i < m || j < n) { + let [a, b] = [0, 0]; + while (i < m && version1[i] !== '.') { + a = a * 10 + +version1[i]; + i++; } - if (n1> n2) { - return 1; + while (j < n && version2[j] !== '.') { + b = b * 10 + +version2[j]; + j++; } + if (a !== b) { + return a < b ? -1 : 1; + } + i++; + j++; } return 0; } ``` +#### Rust + +```rust +impl Solution { + pub fn compare_version(version1: String, version2: String) -> i32 { + let (bytes1, bytes2) = (version1.as_bytes(), version2.as_bytes()); + let (m, n) = (bytes1.len(), bytes2.len()); + let (mut i, mut j) = (0, 0); + + while i < m || j < n { + let mut a = 0; + let mut b = 0; + + while i < m && bytes1[i] != b'.' { + a = a * 10 + (bytes1[i] - b'0') as i32; + i += 1; + } + while j < n && bytes2[j] != b'.' { + b = b * 10 + (bytes2[j] - b'0') as i32; + j += 1; + } + + if a != b { + return if a < b { -1 } else { 1 }; + } + + i += 1; + j += 1; + } + + 0 + } +} +``` + #### C# ```cs diff --git a/solution/0100-0199/0165.Compare Version Numbers/Solution.rs b/solution/0100-0199/0165.Compare Version Numbers/Solution.rs new file mode 100644 index 0000000000000..5aa26838b752c --- /dev/null +++ b/solution/0100-0199/0165.Compare Version Numbers/Solution.rs @@ -0,0 +1,30 @@ +impl Solution { + pub fn compare_version(version1: String, version2: String) -> i32 { + let (bytes1, bytes2) = (version1.as_bytes(), version2.as_bytes()); + let (m, n) = (bytes1.len(), bytes2.len()); + let (mut i, mut j) = (0, 0); + + while i < m || j < n { + let mut a = 0; + let mut b = 0; + + while i < m && bytes1[i] != b'.' { + a = a * 10 + (bytes1[i] - b'0') as i32; + i += 1; + } + while j < n && bytes2[j] != b'.' { + b = b * 10 + (bytes2[j] - b'0') as i32; + j += 1; + } + + if a != b { + return if a < b { -1 } else { 1 }; + } + + i += 1; + j += 1; + } + + 0 + } +} diff --git a/solution/0100-0199/0165.Compare Version Numbers/Solution.ts b/solution/0100-0199/0165.Compare Version Numbers/Solution.ts index 3637a1376ba0b..07b78da5f6c7b 100644 --- a/solution/0100-0199/0165.Compare Version Numbers/Solution.ts +++ b/solution/0100-0199/0165.Compare Version Numbers/Solution.ts @@ -1,14 +1,21 @@ function compareVersion(version1: string, version2: string): number { - const v1 = version1.split('.'); - const v2 = version2.split('.'); - for (let i = 0; i < Math.max(v1.length, v2.length); ++i) { - const [n1, n2] = [+v1[i] || 0, +v2[i] || 0]; - if (n1 < n2) { - return -1; + const [m, n] = [version1.length, version2.length]; + let [i, j] = [0, 0]; + while (i < m || j < n) { + let [a, b] = [0, 0]; + while (i < m && version1[i] !== '.') { + a = a * 10 + +version1[i]; + i++; } - if (n1> n2) { - return 1; + while (j < n && version2[j] !== '.') { + b = b * 10 + +version2[j]; + j++; } + if (a !== b) { + return a < b ? -1 : 1; + } + i++; + j++; } return 0; }