Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions solution/0100-0199/0165.Compare Version Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 57 additions & 9 deletions solution/0100-0199/0165.Compare Version Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ tags:

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions solution/0100-0199/0165.Compare Version Numbers/Solution.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
23 changes: 15 additions & 8 deletions solution/0100-0199/0165.Compare Version Numbers/Solution.ts
Original file line number Diff line number Diff line change
@@ -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;
}