Skip to content

Commit c97c66d

Browse files
committed
feat: update solutions to lc problem: No.0013
No.0013.Roman to Integer
1 parent aeeb294 commit c97c66d

File tree

8 files changed

+157
-133
lines changed

8 files changed

+157
-133
lines changed

solution/0000-0099/0012.Integer to Roman/README_EN.md

-4
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,6 @@ M 1000</pre>
6262

6363
## Solutions
6464

65-
**Approach 1: Simulation**
66-
67-
Time complexity $O(1)$, Space complexity $O(1)$.
68-
6965
<!-- tabs:start -->
7066

7167
### **Python3**

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

+61-41
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ M 1000</pre>
8080

8181
## 解法
8282

83-
**方法一:模拟**
83+
**方法一:哈希表 + 模拟**
8484

85-
因为字符串的长度 $1 \leq s.length \leq 15$,故时间复杂度为 $O(1)$,空间复杂度为 $O(1)$。
85+
我们先用哈希表 $d$ 记录每个字符对应的数值,然后从左到右遍历字符串 $s$,如果当前字符对应的数值小于右边字符对应的数值,则减去当前字符对应的数值,否则加上当前字符对应的数值。
86+
87+
时间复杂度 $(n)$,空间复杂度 $O(m)$。其中 $n$ 和 $m$ 分别为字符串 $s$ 的长度和字符集的大小。
8688

8789
<!-- 这里可写通用的实现逻辑 -->
8890

@@ -95,15 +97,15 @@ M 1000</pre>
9597
```python
9698
class Solution:
9799
def romanToInt(self, s: str) -> int:
98-
romans = {'I': 1, 'V': 5, 'X': 10,
99-
'L': 50, 'C': 100, 'D': 500, 'M': 1000}
100100
ans = 0
101-
for i in range(len(s) - 1):
102-
if romans[s[i]] < romans[s[i + 1]]:
103-
ans -= romans[s[i]]
101+
d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
102+
for a, b in pairwise(s):
103+
if d[a] < d[b]:
104+
ans -= d[a]
104105
else:
105-
ans += romans[s[i]]
106-
return ans + romans[s[-1]]
106+
ans += d[a]
107+
ans += d[s[-1]]
108+
return ans
107109
```
108110

109111
### **Java**
@@ -113,31 +115,23 @@ class Solution:
113115
```java
114116
class Solution {
115117
public int romanToInt(String s) {
116-
Map<String, Integer> nums = new HashMap<>();
117-
nums.put("M", 1000);
118-
nums.put("CM", 900);
119-
nums.put("D", 500);
120-
nums.put("CD", 400);
121-
nums.put("C", 100);
122-
nums.put("XC", 90);
123-
nums.put("L", 50);
124-
nums.put("XL", 40);
125-
nums.put("X", 10);
126-
nums.put("IX", 9);
127-
nums.put("V", 5);
128-
nums.put("IV", 4);
129-
nums.put("I", 1);
130-
int res = 0;
131-
for (int i = 0; i < s.length();) {
132-
if (i + 1 < s.length() && nums.get(s.substring(i, i + 2)) != null) {
133-
res += nums.get(s.substring(i, i + 2));
134-
i += 2;
118+
String cs = "IVXLCDM";
119+
int[] vs = {1, 5, 10, 50, 100, 500, 1000};
120+
Map<Character, Integer> d = new HashMap<>();
121+
for (int i = 0; i < vs.length; ++i) {
122+
d.put(cs.charAt(i), vs[i]);
123+
}
124+
int ans = 0;
125+
int n = s.length();
126+
for (int i = 0; i < n - 1; ++i) {
127+
if (d.get(s.charAt(i)) < d.get(s.charAt(i + 1))) {
128+
ans -= d.get(s.charAt(i));
135129
} else {
136-
res += nums.get(s.substring(i, i + 1));
137-
i += 1;
130+
ans += d.get(s.charAt(i));
138131
}
139132
}
140-
return res;
133+
ans += d.get(s.charAt(n - 1));
134+
return ans;
141135
}
142136
}
143137
```
@@ -148,7 +142,7 @@ class Solution {
148142
class Solution {
149143
public:
150144
int romanToInt(string s) {
151-
unordered_map<char, int> nums {
145+
unordered_map<char, int> nums{
152146
{'I', 1},
153147
{'V', 5},
154148
{'X', 10},
@@ -159,10 +153,11 @@ public:
159153
};
160154
int ans = 0;
161155
for (int i = 0; i < s.size() - 1; ++i) {
162-
if (nums[s[i]] < nums[s[i + 1]])
156+
if (nums[s[i]] < nums[s[i + 1]]) {
163157
ans -= nums[s[i]];
164-
else
158+
} else {
165159
ans += nums[s[i]];
160+
}
166161
}
167162
return ans + nums[s.back()];
168163
}
@@ -172,20 +167,45 @@ public:
172167
### **Go**
173168
174169
```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
170+
func romanToInt(s string) (ans int) {
171+
d := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
178172
for i := 0; i < len(s)-1; i++ {
179-
if romans[s[i]] < romans[s[i+1]] {
180-
ans -= romans[s[i]]
173+
if d[s[i]] < d[s[i+1]] {
174+
ans -= d[s[i]]
181175
} else {
182-
ans += romans[s[i]]
176+
ans += d[s[i]]
183177
}
184178
}
185-
return ans + romans[s[len(s)-1]]
179+
ans += d[s[len(s)-1]]
180+
return
186181
}
187182
```
188183

184+
### **JavaScript**
185+
186+
```js
187+
const romanToInt = function (s) {
188+
const d = {
189+
I: 1,
190+
V: 5,
191+
X: 10,
192+
L: 50,
193+
C: 100,
194+
D: 500,
195+
M: 1000,
196+
};
197+
let ans = 0;
198+
for (let i = 0; i < s.length; ++i) {
199+
if (d[s[i]] < d[s[i + 1]]) {
200+
ans -= d[s[i]];
201+
} else {
202+
ans += d[s[i]];
203+
}
204+
}
205+
return ans;
206+
};
207+
```
208+
189209
### **PHP**
190210

191211
```php

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

+57-43
Original file line numberDiff line numberDiff line change
@@ -64,58 +64,46 @@ M 1000</pre>
6464

6565
## Solutions
6666

67-
**Approach 1: Simulation**
68-
69-
Time complexity $O(1)$, Space complexity $O(1)$.
70-
7167
<!-- tabs:start -->
7268

7369
### **Python3**
7470

7571
```python
7672
class Solution:
7773
def romanToInt(self, s: str) -> int:
78-
romans = {'I': 1, 'V': 5, 'X': 10,
79-
'L': 50, 'C': 100, 'D': 500, 'M': 1000}
8074
ans = 0
81-
for i in range(len(s) - 1):
82-
if romans[s[i]] < romans[s[i + 1]]:
83-
ans -= romans[s[i]]
75+
d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
76+
for a, b in pairwise(s):
77+
if d[a] < d[b]:
78+
ans -= d[a]
8479
else:
85-
ans += romans[s[i]]
86-
return ans + romans[s[-1]]
80+
ans += d[a]
81+
ans += d[s[-1]]
82+
return ans
8783
```
8884

8985
### **Java**
9086

9187
```java
9288
class Solution {
9389
public int romanToInt(String s) {
94-
Map<String, Integer> nums = new HashMap<>();
95-
nums.put("M", 1000);
96-
nums.put("CM", 900);
97-
nums.put("D", 500);
98-
nums.put("CD", 400);
99-
nums.put("C", 100);
100-
nums.put("XC", 90);
101-
nums.put("L", 50);
102-
nums.put("XL", 40);
103-
nums.put("X", 10);
104-
nums.put("IX", 9);
105-
nums.put("V", 5);
106-
nums.put("IV", 4);
107-
nums.put("I", 1);
108-
int res = 0;
109-
for (int i = 0; i < s.length();) {
110-
if (i + 1 < s.length() && nums.get(s.substring(i, i + 2)) != null) {
111-
res += nums.get(s.substring(i, i + 2));
112-
i += 2;
90+
String cs = "IVXLCDM";
91+
int[] vs = {1, 5, 10, 50, 100, 500, 1000};
92+
Map<Character, Integer> d = new HashMap<>();
93+
for (int i = 0; i < vs.length; ++i) {
94+
d.put(cs.charAt(i), vs[i]);
95+
}
96+
int ans = 0;
97+
int n = s.length();
98+
for (int i = 0; i < n - 1; ++i) {
99+
if (d.get(s.charAt(i)) < d.get(s.charAt(i + 1))) {
100+
ans -= d.get(s.charAt(i));
113101
} else {
114-
res += nums.get(s.substring(i, i + 1));
115-
i += 1;
102+
ans += d.get(s.charAt(i));
116103
}
117104
}
118-
return res;
105+
ans += d.get(s.charAt(n - 1));
106+
return ans;
119107
}
120108
}
121109
```
@@ -126,7 +114,7 @@ class Solution {
126114
class Solution {
127115
public:
128116
int romanToInt(string s) {
129-
unordered_map<char, int> nums {
117+
unordered_map<char, int> nums{
130118
{'I', 1},
131119
{'V', 5},
132120
{'X', 10},
@@ -137,10 +125,11 @@ public:
137125
};
138126
int ans = 0;
139127
for (int i = 0; i < s.size() - 1; ++i) {
140-
if (nums[s[i]] < nums[s[i + 1]])
128+
if (nums[s[i]] < nums[s[i + 1]]) {
141129
ans -= nums[s[i]];
142-
else
130+
} else {
143131
ans += nums[s[i]];
132+
}
144133
}
145134
return ans + nums[s.back()];
146135
}
@@ -150,20 +139,45 @@ public:
150139
### **Go**
151140
152141
```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
142+
func romanToInt(s string) (ans int) {
143+
d := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
156144
for i := 0; i < len(s)-1; i++ {
157-
if romans[s[i]] < romans[s[i+1]] {
158-
ans -= romans[s[i]]
145+
if d[s[i]] < d[s[i+1]] {
146+
ans -= d[s[i]]
159147
} else {
160-
ans += romans[s[i]]
148+
ans += d[s[i]]
161149
}
162150
}
163-
return ans + romans[s[len(s)-1]]
151+
ans += d[s[len(s)-1]]
152+
return
164153
}
165154
```
166155

156+
### **JavaScript**
157+
158+
```js
159+
const romanToInt = function (s) {
160+
const d = {
161+
I: 1,
162+
V: 5,
163+
X: 10,
164+
L: 50,
165+
C: 100,
166+
D: 500,
167+
M: 1000,
168+
};
169+
let ans = 0;
170+
for (let i = 0; i < s.length; ++i) {
171+
if (d[s[i]] < d[s[i + 1]]) {
172+
ans -= d[s[i]];
173+
} else {
174+
ans += d[s[i]];
175+
}
176+
}
177+
return ans;
178+
};
179+
```
180+
167181
### **PHP**
168182

169183
```php

solution/0000-0099/0013.Roman to Integer/Solution.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ class Solution {
1212
};
1313
int ans = 0;
1414
for (int i = 0; i < s.size() - 1; ++i) {
15-
if (nums[s[i]] < nums[s[i + 1]])
15+
if (nums[s[i]] < nums[s[i + 1]]) {
1616
ans -= nums[s[i]];
17-
else
17+
} else {
1818
ans += nums[s[i]];
19+
}
1920
}
2021
return ans + nums[s.back()];
2122
}
22-
};
23+
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
func romanToInt(s string) int {
2-
romans := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
3-
ans := 0
1+
func romanToInt(s string) (ans int) {
2+
d := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
43
for i := 0; i < len(s)-1; i++ {
5-
if romans[s[i]] < romans[s[i+1]] {
6-
ans -= romans[s[i]]
4+
if d[s[i]] < d[s[i+1]] {
5+
ans -= d[s[i]]
76
} else {
8-
ans += romans[s[i]]
7+
ans += d[s[i]]
98
}
109
}
11-
return ans + romans[s[len(s)-1]]
10+
ans += d[s[len(s)-1]]
11+
return
1212
}

0 commit comments

Comments
 (0)