Skip to content

Commit 7939898

Browse files
committed
feat: add solutions to lc problem: No.0013
No.0013.Roman to Integer
1 parent 183a942 commit 7939898

File tree

8 files changed

+167
-87
lines changed

8 files changed

+167
-87
lines changed

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

+57-29
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,8 @@ M 1000</pre>
9797
```python
9898
class Solution:
9999
def romanToInt(self, s: str) -> int:
100-
ans = 0
101100
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]
105-
else:
106-
ans += d[a]
107-
ans += d[s[-1]]
108-
return ans
101+
return sum((-1 if d[a] < d[b] else 1) * d[a] for a, b in pairwise(s)) + d[s[-1]]
109102
```
110103

111104
### **Java**
@@ -121,16 +114,12 @@ class Solution {
121114
for (int i = 0; i < vs.length; ++i) {
122115
d.put(cs.charAt(i), vs[i]);
123116
}
124-
int ans = 0;
125117
int n = s.length();
118+
int ans = d.get(s.charAt(n - 1));
126119
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));
129-
} else {
130-
ans += d.get(s.charAt(i));
131-
}
120+
int sign = d.get(s.charAt(i)) < d.get(s.charAt(i + 1)) ? -1 : 1;
121+
ans += sign * d.get(s.charAt(i));
132122
}
133-
ans += d.get(s.charAt(n - 1));
134123
return ans;
135124
}
136125
}
@@ -151,15 +140,12 @@ public:
151140
{'D', 500},
152141
{'M', 1000},
153142
};
154-
int ans = 0;
143+
int ans = nums[s.back()];
155144
for (int i = 0; i < s.size() - 1; ++i) {
156-
if (nums[s[i]] < nums[s[i + 1]]) {
157-
ans -= nums[s[i]];
158-
} else {
159-
ans += nums[s[i]];
160-
}
145+
int sign = nums[s[i]] < nums[s[i + 1]] ? -1 : 1;
146+
ans += sign * nums[s[i]];
161147
}
162-
return ans + nums[s.back()];
148+
return ans;
163149
}
164150
};
165151
```
@@ -181,6 +167,28 @@ func romanToInt(s string) (ans int) {
181167
}
182168
```
183169

170+
### **TypeScript**
171+
172+
```ts
173+
function romanToInt(s: string): number {
174+
const d: Map<string, number> = new Map([
175+
['I', 1],
176+
['V', 5],
177+
['X', 10],
178+
['L', 50],
179+
['C', 100],
180+
['D', 500],
181+
['M', 1000],
182+
]);
183+
let ans: number = d.get(s[s.length - 1])!;
184+
for (let i = 0; i < s.length - 1; ++i) {
185+
const sign = d.get(s[i])! < d.get(s[i + 1])! ? -1 : 1;
186+
ans += sign * d.get(s[i])!;
187+
}
188+
return ans;
189+
}
190+
```
191+
184192
### **JavaScript**
185193

186194
```js
@@ -194,18 +202,38 @@ const romanToInt = function (s) {
194202
D: 500,
195203
M: 1000,
196204
};
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-
}
205+
let ans = d[s[s.length - 1]];
206+
for (let i = 0; i < s.length - 1; ++i) {
207+
const sign = d[s[i]] < d[s[i + 1]] ? -1 : 1;
208+
ans += sign * d[s[i]];
204209
}
205210
return ans;
206211
};
207212
```
208213

214+
### **C#**
215+
216+
```cs
217+
public class Solution {
218+
public int RomanToInt(string s) {
219+
Dictionary<char, int> d = new Dictionary<char, int>();
220+
d.Add('I', 1);
221+
d.Add('V', 5);
222+
d.Add('X', 10);
223+
d.Add('L', 50);
224+
d.Add('C', 100);
225+
d.Add('D', 500);
226+
d.Add('M', 1000);
227+
int ans = d[s[s.Length - 1]];
228+
for (int i = 0; i < s.Length - 1; ++i) {
229+
int sign = d[s[i]] < d[s[i + 1]] ? -1 : 1;
230+
ans += sign * d[s[i]];
231+
}
232+
return ans;
233+
}
234+
}
235+
```
236+
209237
### **PHP**
210238

211239
```php

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

+63-29
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,21 @@ M 1000</pre>
6464

6565
## Solutions
6666

67+
**Approach 1: Hash table + simulation**
68+
69+
We first use a hash table $d$ to record the numerical value corresponding to each character, and then traverse the string $s$ from left to right. If the numerical value corresponding to the current character is less than the numerical value corresponding to the right character, subtract the numerical value corresponding to the current character, otherwise add the numerical value corresponding to the current character.
70+
71+
The time complexity is $O(n)$ and the space complexity is $O(m)$. Where $n$ and $m$ are the lengths of the string $s$ and the size of the character set, respectively.
72+
6773
<!-- tabs:start -->
6874

6975
### **Python3**
7076

7177
```python
7278
class Solution:
7379
def romanToInt(self, s: str) -> int:
74-
ans = 0
7580
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]
79-
else:
80-
ans += d[a]
81-
ans += d[s[-1]]
82-
return ans
81+
return sum((-1 if d[a] < d[b] else 1) * d[a] for a, b in pairwise(s)) + d[s[-1]]
8382
```
8483

8584
### **Java**
@@ -93,16 +92,12 @@ class Solution {
9392
for (int i = 0; i < vs.length; ++i) {
9493
d.put(cs.charAt(i), vs[i]);
9594
}
96-
int ans = 0;
9795
int n = s.length();
96+
int ans = d.get(s.charAt(n - 1));
9897
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));
101-
} else {
102-
ans += d.get(s.charAt(i));
103-
}
98+
int sign = d.get(s.charAt(i)) < d.get(s.charAt(i + 1)) ? -1 : 1;
99+
ans += sign * d.get(s.charAt(i));
104100
}
105-
ans += d.get(s.charAt(n - 1));
106101
return ans;
107102
}
108103
}
@@ -123,15 +118,12 @@ public:
123118
{'D', 500},
124119
{'M', 1000},
125120
};
126-
int ans = 0;
121+
int ans = nums[s.back()];
127122
for (int i = 0; i < s.size() - 1; ++i) {
128-
if (nums[s[i]] < nums[s[i + 1]]) {
129-
ans -= nums[s[i]];
130-
} else {
131-
ans += nums[s[i]];
132-
}
123+
int sign = nums[s[i]] < nums[s[i + 1]] ? -1 : 1;
124+
ans += sign * nums[s[i]];
133125
}
134-
return ans + nums[s.back()];
126+
return ans;
135127
}
136128
};
137129
```
@@ -153,6 +145,28 @@ func romanToInt(s string) (ans int) {
153145
}
154146
```
155147

148+
### **TypeScript**
149+
150+
```ts
151+
function romanToInt(s: string): number {
152+
const d: Map<string, number> = new Map([
153+
['I', 1],
154+
['V', 5],
155+
['X', 10],
156+
['L', 50],
157+
['C', 100],
158+
['D', 500],
159+
['M', 1000],
160+
]);
161+
let ans: number = d.get(s[s.length - 1])!;
162+
for (let i = 0; i < s.length - 1; ++i) {
163+
const sign = d.get(s[i])! < d.get(s[i + 1])! ? -1 : 1;
164+
ans += sign * d.get(s[i])!;
165+
}
166+
return ans;
167+
}
168+
```
169+
156170
### **JavaScript**
157171

158172
```js
@@ -166,18 +180,38 @@ const romanToInt = function (s) {
166180
D: 500,
167181
M: 1000,
168182
};
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-
}
183+
let ans = d[s[s.length - 1]];
184+
for (let i = 0; i < s.length - 1; ++i) {
185+
const sign = d[s[i]] < d[s[i + 1]] ? -1 : 1;
186+
ans += sign * d[s[i]];
176187
}
177188
return ans;
178189
};
179190
```
180191

192+
### **C#**
193+
194+
```cs
195+
public class Solution {
196+
public int RomanToInt(string s) {
197+
Dictionary<char, int> d = new Dictionary<char, int>();
198+
d.Add('I', 1);
199+
d.Add('V', 5);
200+
d.Add('X', 10);
201+
d.Add('L', 50);
202+
d.Add('C', 100);
203+
d.Add('D', 500);
204+
d.Add('M', 1000);
205+
int ans = d[s[s.Length - 1]];
206+
for (int i = 0; i < s.Length - 1; ++i) {
207+
int sign = d[s[i]] < d[s[i + 1]] ? -1 : 1;
208+
ans += sign * d[s[i]];
209+
}
210+
return ans;
211+
}
212+
}
213+
```
214+
181215
### **PHP**
182216

183217
```php

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ class Solution {
1010
{'D', 500},
1111
{'M', 1000},
1212
};
13-
int ans = 0;
13+
int ans = nums[s.back()];
1414
for (int i = 0; i < s.size() - 1; ++i) {
15-
if (nums[s[i]] < nums[s[i + 1]]) {
16-
ans -= nums[s[i]];
17-
} else {
18-
ans += nums[s[i]];
19-
}
15+
int sign = nums[s[i]] < nums[s[i + 1]] ? -1 : 1;
16+
ans += sign * nums[s[i]];
2017
}
21-
return ans + nums[s.back()];
18+
return ans;
2219
}
2320
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int RomanToInt(string s) {
3+
Dictionary<char, int> d = new Dictionary<char, int>();
4+
d.Add('I', 1);
5+
d.Add('V', 5);
6+
d.Add('X', 10);
7+
d.Add('L', 50);
8+
d.Add('C', 100);
9+
d.Add('D', 500);
10+
d.Add('M', 1000);
11+
int ans = d[s[s.Length - 1]];
12+
for (int i = 0; i < s.Length - 1; ++i) {
13+
int sign = d[s[i]] < d[s[i + 1]] ? -1 : 1;
14+
ans += sign * d[s[i]];
15+
}
16+
return ans;
17+
}
18+
}

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

+3-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ public int romanToInt(String s) {
66
for (int i = 0; i < vs.length; ++i) {
77
d.put(cs.charAt(i), vs[i]);
88
}
9-
int ans = 0;
109
int n = s.length();
10+
int ans = d.get(s.charAt(n - 1));
1111
for (int i = 0; i < n - 1; ++i) {
12-
if (d.get(s.charAt(i)) < d.get(s.charAt(i + 1))) {
13-
ans -= d.get(s.charAt(i));
14-
} else {
15-
ans += d.get(s.charAt(i));
16-
}
12+
int sign = d.get(s.charAt(i)) < d.get(s.charAt(i + 1)) ? -1 : 1;
13+
ans += sign * d.get(s.charAt(i));
1714
}
18-
ans += d.get(s.charAt(n - 1));
1915
return ans;
2016
}
2117
}

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ const romanToInt = function (s) {
88
D: 500,
99
M: 1000,
1010
};
11-
let ans = 0;
12-
for (let i = 0; i < s.length; ++i) {
13-
if (d[s[i]] < d[s[i + 1]]) {
14-
ans -= d[s[i]];
15-
} else {
16-
ans += d[s[i]];
17-
}
11+
let ans = d[s[s.length - 1]];
12+
for (let i = 0; i < s.length - 1; ++i) {
13+
const sign = d[s[i]] < d[s[i + 1]] ? -1 : 1;
14+
ans += sign * d[s[i]];
1815
}
1916
return ans;
2017
};
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
class Solution:
22
def romanToInt(self, s: str) -> int:
3-
ans = 0
43
d = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
5-
for a, b in pairwise(s):
6-
if d[a] < d[b]:
7-
ans -= d[a]
8-
else:
9-
ans += d[a]
10-
ans += d[s[-1]]
11-
return ans
4+
return sum((-1 if d[a] < d[b] else 1) * d[a] for a, b in pairwise(s)) + d[s[-1]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function romanToInt(s: string): number {
2+
const d: Map<string, number> = new Map([
3+
['I', 1],
4+
['V', 5],
5+
['X', 10],
6+
['L', 50],
7+
['C', 100],
8+
['D', 500],
9+
['M', 1000],
10+
]);
11+
let ans: number = d.get(s[s.length - 1])!;
12+
for (let i = 0; i < s.length - 1; ++i) {
13+
const sign = d.get(s[i])! < d.get(s[i + 1])! ? -1 : 1;
14+
ans += sign * d.get(s[i])!;
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)