Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add solutions to lc problem: No.0165 #4744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 1 commit into main from dev
Sep 22, 2025
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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
View file Open in desktop
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;
}

AltStyle によって変換されたページ (->オリジナル) /