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 bfbb28c

Browse files
committed
feat: update solutions to lc problem: No.0013
No.0013.Roman to Integer
1 parent 601a3b9 commit bfbb28c

File tree

4 files changed

+76
-108
lines changed

4 files changed

+76
-108
lines changed

‎solution/0000-0099/0013.Roman to Integer/README.md‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ M 1000</pre>
8080

8181
## 解法
8282

83+
**方法一:模拟**
84+
85+
因为字符串的长度 1ドル \leq s.length \leq 15,ドル故时间复杂度为 $O(1),ドル空间复杂度为 $O(1)$。
86+
8387
<!-- 这里可写通用的实现逻辑 -->
8488

8589
<!-- tabs:start -->
@@ -91,30 +95,15 @@ M 1000</pre>
9195
```python
9296
class Solution:
9397
def romanToInt(self, s: str) -> int:
94-
nums = {
95-
'M': 1000,
96-
'CM': 900,
97-
'D': 500,
98-
'CD': 400,
99-
'C': 100,
100-
'XC': 90,
101-
'L': 50,
102-
'XL': 40,
103-
'X': 10,
104-
'IX': 9,
105-
'V': 5,
106-
'IV': 4,
107-
'I': 1,
108-
}
109-
i = res = 0
110-
while i < len(s):
111-
if i + 1 < len(s) and s[i : i + 2] in nums:
112-
res += nums[s[i : i + 2]]
113-
i += 2
98+
romans = {'I': 1, 'V': 5, 'X': 10,
99+
'L': 50, 'C': 100, 'D': 500, 'M': 1000}
100+
ans = 0
101+
for i in range(len(s)-1):
102+
if romans[s[i]] < romans[s[i+1]]:
103+
ans -= romans[s[i]]
114104
else:
115-
res += nums[s[i : i + 1]]
116-
i += 1
117-
return res
105+
ans += romans[s[i]]
106+
return ans+romans[s[-1]]
118107
```
119108

120109
### **Java**
@@ -180,6 +169,23 @@ public:
180169
};
181170
```
182171
172+
### **Go**
173+
174+
```go
175+
func romanToInt(s string) int {
176+
romans := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
177+
ans := 0
178+
for i := 0; i < len(s)-1; i++ {
179+
if romans[s[i]] < romans[s[i+1]] {
180+
ans -= romans[s[i]]
181+
} else {
182+
ans += romans[s[i]]
183+
}
184+
}
185+
return ans + romans[s[len(s)-1]]
186+
}
187+
```
188+
183189
### **...**
184190

185191
```

‎solution/0000-0099/0013.Roman to Integer/README_EN.md‎

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,37 +64,26 @@ M 1000</pre>
6464

6565
## Solutions
6666

67+
**Approach 1: Simulation**
68+
69+
Time complexity $O(1),ドル Space complexity $O(1)$.
70+
6771
<!-- tabs:start -->
6872

6973
### **Python3**
7074

7175
```python
7276
class Solution:
7377
def romanToInt(self, s: str) -> int:
74-
nums = {
75-
'M': 1000,
76-
'CM': 900,
77-
'D': 500,
78-
'CD': 400,
79-
'C': 100,
80-
'XC': 90,
81-
'L': 50,
82-
'XL': 40,
83-
'X': 10,
84-
'IX': 9,
85-
'V': 5,
86-
'IV': 4,
87-
'I': 1,
88-
}
89-
i = res = 0
90-
while i < len(s):
91-
if i + 1 < len(s) and s[i : i + 2] in nums:
92-
res += nums[s[i : i + 2]]
93-
i += 2
78+
romans = {'I': 1, 'V': 5, 'X': 10,
79+
'L': 50, 'C': 100, 'D': 500, 'M': 1000}
80+
ans = 0
81+
for i in range(len(s)-1):
82+
if romans[s[i]] < romans[s[i+1]]:
83+
ans -= romans[s[i]]
9484
else:
95-
res += nums[s[i : i + 1]]
96-
i += 1
97-
return res
85+
ans += romans[s[i]]
86+
return ans+romans[s[-1]]
9887
```
9988

10089
### **Java**
@@ -158,6 +147,23 @@ public:
158147
};
159148
```
160149
150+
### **Go**
151+
152+
```go
153+
func romanToInt(s string) int {
154+
romans := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
155+
ans := 0
156+
for i := 0; i < len(s)-1; i++ {
157+
if romans[s[i]] < romans[s[i+1]] {
158+
ans -= romans[s[i]]
159+
} else {
160+
ans += romans[s[i]]
161+
}
162+
}
163+
return ans + romans[s[len(s)-1]]
164+
}
165+
```
166+
161167
### **...**
162168

163169
```
Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,12 @@
1-
// 字符 数值
2-
// I 1
3-
// V 5
4-
// X 10
5-
// L 50
6-
// C 100
7-
// D 500
8-
// M 1000
9-
var table = make(map[string]int)
10-
11-
func init() {
12-
table["I"] = 1
13-
table["IV"] = 4
14-
table["V"] = 5
15-
table["IX"] = 9
16-
table["X"] = 10
17-
table["XL"] = 40
18-
table["L"] = 50
19-
table["XC"] = 90
20-
table["C"] = 100
21-
table["CD"] = 400
22-
table["D"] = 500
23-
table["CM"] = 900
24-
table["M"] = 1000
25-
}
26-
271
func romanToInt(s string) int {
28-
var result int
29-
lenS := len(s)
30-
for i:=0; i<lenS; i++ {
31-
if i < lenS - 1 {
32-
if v, exist := table[s[i:i+2]]; exist {
33-
result += v
34-
i++
35-
continue
36-
}
37-
}
38-
result += table[string(s[i])]
39-
}
40-
return result
2+
romans := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
3+
ans := 0
4+
for i := 0; i < len(s)-1; i++ {
5+
if romans[s[i]] < romans[s[i+1]] {
6+
ans -= romans[s[i]]
7+
} else {
8+
ans += romans[s[i]]
9+
}
10+
}
11+
return ans + romans[s[len(s)-1]]
4112
}
Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,11 @@
11
class Solution:
22
def romanToInt(self, s: str) -> int:
3-
nums = {
4-
'M': 1000,
5-
'CM': 900,
6-
'D': 500,
7-
'CD': 400,
8-
'C': 100,
9-
'XC': 90,
10-
'L': 50,
11-
'XL': 40,
12-
'X': 10,
13-
'IX': 9,
14-
'V': 5,
15-
'IV': 4,
16-
'I': 1,
17-
}
18-
i = res = 0
19-
while i < len(s):
20-
if i + 1 < len(s) and s[i : i + 2] in nums:
21-
res += nums[s[i : i + 2]]
22-
i += 2
3+
romans = {'I': 1, 'V': 5, 'X': 10,
4+
'L': 50, 'C': 100, 'D': 500, 'M': 1000}
5+
ans = 0
6+
for i in range(len(s)-1):
7+
if romans[s[i]] < romans[s[i+1]]:
8+
ans -= romans[s[i]]
239
else:
24-
res += nums[s[i : i + 1]]
25-
i += 1
26-
return res
10+
ans += romans[s[i]]
11+
return ans+romans[s[-1]]

0 commit comments

Comments
(0)

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