Skip to content

Commit 3febc6c

Browse files
committed
feat: commit solution
1 parent ea8939e commit 3febc6c

File tree

13 files changed

+522
-21
lines changed

13 files changed

+522
-21
lines changed

Diff for: README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
leetcode 题解,记录自己的 leetcode 解题之路。
2121

22-
**本仓库具有已下特点**
22+
**本项目具有如下特点**
2323

2424
- 可根据题目难易度与题型标签进行`快速索引`
25-
- 带有leetcode问题链接的`说明``示例`
26-
- 包括leetcode`解题思路`和具体的`代码实现`
25+
- 包含leetcode问题链接的`说明``示例`
26+
- 包含leetcode`解题思路`和具体的`代码实现`
2727
- 包含单元测试,已提交的问题均已通过leetcode各自的测试用例。
2828
- 可以直接通过编辑页面实现`Pull requests`
2929

Diff for: _sidebar.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11

22
- 🏠  速览
33
- [项目介绍](README.md "The greatest guide in the world")
4-
- [快速索引](# "按照题型或难易度快速检索题目.")
5-
- [题型难度](index-type.md)
6-
- [题型标签](index-tags.md)
4+
- [快速索引](index-type.md "按照题型或难易度快速检索题目.")
5+
- [- 题型难度](index-type.md)
6+
- [- 题型标签](index-tags.md)
77
- [贡献者列表](contributor.md)
88

99
- 📚  题目列表
1010
- [1.两数之和 ✅](solution/1-99/0001.two-sum/)
1111
- [2.两数相加](solution/1-99/0002.add-two-numbers/)
1212
- [7.整数反转 ✅](solution/1-99/0007.reverse-integer/)
1313
- [9.回文数 ✅](solution/1-99/0009.palindrome-number/)
14+
- [13.罗马数字转整数 ✅](solution/1-99/0013.roman-to-integer/)
15+
- [20.有效的括号 ✅](solution/1-99/0020.valid-parentheses/)
16+
- [26.删除排序数组中的重复项 ✅](solution/1-99/0026.remove-duplicates-from-sorted-array/)
17+
1418

1519

1620

Diff for: index-tags.md

+27-12
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,58 @@
22

33
<!-- tabs:start -->
44

5-
#### ****
6-
7-
#### ****
8-
9-
#### **排序**
10-
11-
#### **队列**
12-
13-
#### **位运算**
14-
15-
#### ****
16-
175
#### **数学**
186

197
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
208
| --- | --- | --- | --- | --- |
219
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
2210
| [7](https://leetcode-cn.com/problems/reverse-integer) | [整数反转](/solution/1-99/0007.reverse-integer/) | `数学` | <font color=green>简单</font> ||
11+
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
2312

2413
#### **字符串**
2514

15+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
16+
| --- | --- | --- | --- | --- |
17+
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
18+
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
19+
2620
#### **数组**
2721

2822
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
2923
| --- | --- | --- | --- | --- |
3024
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
25+
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
3126

3227
#### **哈希表**
3328

3429
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
3530
| --- | --- | --- | --- | --- |
3631
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
3732

33+
#### ****
34+
35+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
36+
| --- | --- | --- | --- | --- |
37+
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
38+
39+
#### ****
40+
41+
#### **排序**
42+
43+
#### **队列**
44+
45+
#### **位运算**
46+
47+
#### ****
48+
3849
#### **二分查找**
3950

4051
#### **双指针**
4152

53+
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
54+
| --- | --- | --- | --- | --- |
55+
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
56+
4257
#### **递归**
4358

4459
#### **深度优先搜素**

Diff for: index-type.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
1111
| [7](https://leetcode-cn.com/problems/reverse-integer) | [整数反转](/solution/1-99/0007.reverse-integer/) | `数学` | <font color=green>简单</font> ||
1212
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
13-
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> |
13+
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
1414
| [14](https://leetcode-cn.com/problems/longest-common-prefix) | [最长公共前缀](/solution/1-99/0014.longest-common-prefix/) | `字符串` | <font color=green>简单</font> |
15-
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> |
15+
| [20](https://leetcode-cn.com/problems/valid-parentheses) | [有效的括号](/solution/1-99/0020.valid-parentheses/) | ``,`字符串` | <font color=green>简单</font> ||
1616
| [21](https://leetcode-cn.com/problems/merge-two-sorted-lists) | [合并两个有序链表](/solution/1-99/0021.merge-two-sorted-lists/) | `链表` | <font color=green>简单</font> |
17-
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> |
17+
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1818
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> |
1919
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> |
2020
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> |

Diff for: solution/1-99/0013.roman-to-integer/README.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# [13.罗马数字转整数](https://leetcode-cn.com/problems/palindrome-number)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>罗马数字包含以下七种字符:&nbsp;<code>I</code>,&nbsp;<code>V</code>,&nbsp;<code>X</code>,&nbsp;<code>L</code>,<code>C</code>,<code>D</code>&nbsp;&nbsp;<code>M</code>。</p>
7+
8+
<pre><strong>字符</strong> <strong>数值</strong>
9+
I 1
10+
V 5
11+
X 10
12+
L 50
13+
C 100
14+
D 500
15+
M 1000</pre>
16+
17+
<p>例如, 罗马数字 2 写做&nbsp;<code>II</code>&nbsp;,即为两个并列的 1。12 写做&nbsp;<code>XII</code>&nbsp;,即为&nbsp;<code>X</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。 27 写做&nbsp;&nbsp;<code>XXVII</code>, 即为&nbsp;<code>XX</code>&nbsp;+&nbsp;<code>V</code>&nbsp;+&nbsp;<code>II</code>&nbsp;。</p>
18+
19+
<p>通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做&nbsp;<code>IIII</code>,而是&nbsp;<code>IV</code>。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为&nbsp;<code>IX</code>。这个特殊的规则只适用于以下六种情况:</p>
20+
21+
<ul>
22+
<li><code>I</code>&nbsp;可以放在&nbsp;<code>V</code>&nbsp;(5) 和&nbsp;<code>X</code>&nbsp;(10) 的左边,来表示 4 和 9。</li>
23+
<li><code>X</code>&nbsp;可以放在&nbsp;<code>L</code>&nbsp;(50) 和&nbsp;<code>C</code>&nbsp;(100) 的左边,来表示 40 和&nbsp;90。&nbsp;</li>
24+
<li><code>C</code>&nbsp;可以放在&nbsp;<code>D</code>&nbsp;(500) 和&nbsp;<code>M</code>&nbsp;(1000) 的左边,来表示&nbsp;400 和&nbsp;900。</li>
25+
</ul>
26+
27+
<p>给定一个罗马数字,将其转换成整数。输入确保在 1&nbsp;到 3999 的范围内。</p>
28+
29+
<p><strong>示例&nbsp;1:</strong></p>
30+
31+
<pre><strong>输入:</strong>&nbsp;"III"
32+
<strong>输出:</strong> 3</pre>
33+
34+
<p><strong>示例&nbsp;2:</strong></p>
35+
36+
<pre><strong>输入:</strong>&nbsp;"IV"
37+
<strong>输出:</strong> 4</pre>
38+
39+
<p><strong>示例&nbsp;3:</strong></p>
40+
41+
<pre><strong>输入:</strong>&nbsp;"IX"
42+
<strong>输出:</strong> 9</pre>
43+
44+
<p><strong>示例&nbsp;4:</strong></p>
45+
46+
<pre><strong>输入:</strong>&nbsp;"LVIII"
47+
<strong>输出:</strong> 58
48+
<strong>解释:</strong> L = 50, V= 5, III = 3.
49+
</pre>
50+
51+
<p><strong>示例&nbsp;5:</strong></p>
52+
53+
<pre><strong>输入:</strong>&nbsp;"MCMXCIV"
54+
<strong>输出:</strong> 1994
55+
<strong>解释:</strong> M = 1000, CM = 900, XC = 90, IV = 4.</pre>
56+
</div>
57+
58+
59+
### 解题思路
60+
61+
1. 先匹配特殊的字符串,匹配到的话`ret`累加
62+
2. 遍历字符串,如果能在`romanMap`中找到的话`ret`累加
63+
64+
?> 该题解不是最优解!
65+
66+
### 代码实现
67+
68+
<!-- tabs:start -->
69+
70+
#### **Golang**
71+
```go
72+
import (
73+
"strconv"
74+
"strings"
75+
)
76+
77+
func romanToInt(s string) int {
78+
romanMap := map[string]int{
79+
"I": 1,
80+
"V": 5,
81+
"X": 10,
82+
"L": 50,
83+
"C": 100,
84+
"D": 500,
85+
"M": 1000,
86+
}
87+
romanManyMap := map[string]int{
88+
"IV": 4,
89+
"IX": 9,
90+
"XL": 40,
91+
"XC": 90,
92+
"CD": 400,
93+
"CM": 900,
94+
}
95+
var ret int
96+
for k, v := range romanManyMap {
97+
if strings.Contains(s, k) {
98+
s = strings.Replace(s, k, strconv.Itoa(v), -1)
99+
ret += v
100+
}
101+
}
102+
for k := range s {
103+
if romanMap[s[k:k+1]] != 0 {
104+
ret += romanMap[s[k:k+1]]
105+
}
106+
}
107+
return ret
108+
}
109+
```
110+
#### **Java**
111+
112+
#### **Python**
113+
114+
#### **PHP**
115+
116+
<!-- tabs:end -->

Diff for: solution/1-99/0013.roman-to-integer/solution.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
import (
4+
"strconv"
5+
"strings"
6+
)
7+
8+
func romanToInt(s string) int {
9+
romanMap := map[string]int{
10+
"I": 1,
11+
"V": 5,
12+
"X": 10,
13+
"L": 50,
14+
"C": 100,
15+
"D": 500,
16+
"M": 1000,
17+
}
18+
romanManyMap := map[string]int{
19+
"IV": 4,
20+
"IX": 9,
21+
"XL": 40,
22+
"XC": 90,
23+
"CD": 400,
24+
"CM": 900,
25+
}
26+
var ret int
27+
for k, v := range romanManyMap {
28+
if strings.Contains(s, k) {
29+
s = strings.Replace(s, k, strconv.Itoa(v), -1)
30+
ret += v
31+
}
32+
}
33+
for k := range s {
34+
if romanMap[s[k:k+1]] != 0 {
35+
ret += romanMap[s[k:k+1]]
36+
}
37+
}
38+
return ret
39+
}

Diff for: solution/1-99/0013.roman-to-integer/solution_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestRomanToInt(t *testing.T) {
9+
10+
var s string
11+
var ret int
12+
s = "III"
13+
ret = 3
14+
fmt.Printf("x = %v ret = %v\n", s, romanToInt(s))
15+
if romanToInt(s) != ret {
16+
t.Fatalf("case fails: %v\n", ret)
17+
}
18+
19+
s = "IV"
20+
ret = 4
21+
fmt.Printf("x = %v ret = %v\n", s, romanToInt(s))
22+
if romanToInt(s) != ret {
23+
t.Fatalf("case fails: %v\n", ret)
24+
}
25+
26+
s = "IX"
27+
ret = 9
28+
fmt.Printf("x = %v ret = %v\n", s, romanToInt(s))
29+
if romanToInt(s) != ret {
30+
t.Fatalf("case fails: %v\n", ret)
31+
}
32+
33+
s = "LVIII"
34+
ret = 58
35+
fmt.Printf("x = %v ret = %v\n", s, romanToInt(s))
36+
if romanToInt(s) != ret {
37+
t.Fatalf("case fails: %v\n", ret)
38+
}
39+
40+
s = "MCMXCIV"
41+
ret = 1994
42+
fmt.Printf("x = %v ret = %v\n", s, romanToInt(s))
43+
if romanToInt(s) != ret {
44+
t.Fatalf("case fails: %v\n", ret)
45+
}
46+
}

0 commit comments

Comments
 (0)