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

Commit 0c0d18f

Browse files
✨feat: Add 165
1 parent 41ecaaa commit 0c0d18f

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| [59. 螺旋矩阵 II](https://leetcode-cn.com/problems/spiral-matrix-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/spiral-matrix-ii/solution/yi-ti-shuang-jie-xiang-jie-xing-zhuang-j-24x8/) | 中等 | 🤩🤩🤩🤩 |
1616
| [65. 有效数字](https://leetcode-cn.com/problems/valid-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/valid-number/solution/gong-shui-san-xie-zi-fu-chuan-mo-ni-by-a-7cgc/) | 困难 | 🤩🤩🤩 |
1717
| [73. 矩阵置零](https://leetcode-cn.com/problems/set-matrix-zeroes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/set-matrix-zeroes/solution/xiang-jie-fen-san-bu-de-o1-kong-jian-jie-dbxd/) | 中等 | 🤩🤩🤩🤩 |
18+
| [165. 比较版本号](https://leetcode-cn.com/problems/compare-version-numbers/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/compare-version-numbers/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-xsod/) | 中等 | 🤩🤩🤩🤩 |
1819
| [168. Excel表列名称](https://leetcode-cn.com/problems/excel-sheet-column-title/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/excel-sheet-column-title/solution/gong-shui-san-xie-cong-1-kai-shi-de-26-j-g2ur/) | 简单 | 🤩🤩🤩 |
1920
| [171. Excel表列序号](https://leetcode-cn.com/problems/excel-sheet-column-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/excel-sheet-column-number/solution/gong-shui-san-xie-tong-yong-jin-zhi-zhua-y5fm/) | 简单 | 🤩🤩🤩 |
2021
| [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-bits/solution/yi-ti-san-jie-dui-cheng-wei-zhu-wei-fen-ub1hi/) | 简单 | 🤩🤩🤩 |
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[165. 比较版本号](https://leetcode-cn.com/problems/compare-version-numbers/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-xsod/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你两个版本号 version1 和 version2 ,请你比较它们。
10+
11+
版本号由一个或多个修订号组成,各修订号由一个 '.' 连接。每个修订号由 多位数字 组成,可能包含 前导零 。每个版本号至少包含一个字符。修订号从左到右编号,下标从 0 开始,最左边的修订号下标为 0 ,下一个修订号下标为 1 ,以此类推。例如,2.5.33 和 0.1 都是有效的版本号。
12+
13+
比较版本号时,请按从左到右的顺序依次比较它们的修订号。比较修订号时,只需比较 忽略任何前导零后的整数值 。也就是说,修订号 1 和修订号 001 相等 。如果版本号没有指定某个下标处的修订号,则该修订号视为 0 。例如,版本 1.0 小于版本 1.1 ,因为它们下标为 0 的修订号相同,而下标为 1 的修订号分别为 0 和 1 ,0 < 1 。
14+
15+
返回规则如下:
16+
* 如果 version1 > version2 返回 1,
17+
* 如果 version1 < version2 返回 -1,
18+
* 除此之外返回 0。
19+
20+
21+
示例 1:
22+
```
23+
输入:version1 = "1.01", version2 = "1.001"
24+
25+
输出:0
26+
27+
解释:忽略前导零,"01" 和 "001" 都表示相同的整数 "1"
28+
```
29+
示例 2:
30+
```
31+
输入:version1 = "1.0", version2 = "1.0.0"
32+
33+
输出:0
34+
35+
解释:version1 没有指定下标为 2 的修订号,即视为 "0"
36+
```
37+
示例 3:
38+
```
39+
输入:version1 = "0.1", version2 = "1.1"
40+
41+
输出:-1
42+
43+
解释:version1 中下标为 0 的修订号是 "0",version2 中下标为 0 的修订号是 "1" 。0 < 1,所以 version1 < version2
44+
```
45+
示例 4:
46+
```
47+
输入:version1 = "1.0.1", version2 = "1"
48+
49+
输出:1
50+
```
51+
示例 5:
52+
```
53+
输入:version1 = "7.5.2.4", version2 = "7.5.3"
54+
55+
输出:-1
56+
```
57+
58+
提示:
59+
* 1 <= version1.length, version2.length <= 500
60+
* version1 和 version2 仅包含数字和 '.'
61+
* version1 和 version2 都是 有效版本号
62+
* version1 和 version2 的所有修订号都可以存储在 32 位整数 中
63+
64+
---
65+
66+
### 模拟
67+
68+
根据题意,对字符串进行分割,诸位比较「修订号」大小即可。
69+
70+
对于缺省的修订号位置,使用 0ドル$ 进行代指。
71+
72+
代码:
73+
```Java
74+
class Solution {
75+
public int compareVersion(String v1, String v2) {
76+
String[] ss1 = v1.split("\\."), ss2 = v2.split("\\.");
77+
int n = ss1.length, m = ss2.length;
78+
int i = 0, j = 0;
79+
while (i < n || j < m) {
80+
int a = 0, b = 0;
81+
if (i < n) a = Integer.parseInt(ss1[i++]);
82+
if (j < m) b = Integer.parseInt(ss2[j++]);
83+
if (a != b) return a > b ? 1 : -1;
84+
}
85+
return 0;
86+
}
87+
}
88+
```
89+
* 时间复杂度:令 `v1` 长度为 $n,ドル`v2` 长度为 $m$。整体复杂度为 $O(\max(n, m))$
90+
* 空间复杂度:$O(n + m)$
91+
92+
---
93+
94+
### 最后
95+
96+
这是我们「刷穿 LeetCode」系列文章的第 `No.165` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
97+
98+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
99+
100+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
101+
102+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
103+

0 commit comments

Comments
(0)

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