Skip to content

Commit 15ca889

Browse files
committed
feat: update leetcode solutions: No.0013. Roman to Integer
1 parent 8bb1bd2 commit 15ca889

File tree

10 files changed

+164
-42
lines changed

10 files changed

+164
-42
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@
126126

127127
### 数学
128128

129+
- [整数转罗马数字](/solution/0000-0099/0012.Integer%20to%20Roman/README.md)
130+
- [罗马数字转整数](/solution/0000-0099/0013.Roman%20to%20Integer/README.md)
129131
- [只出现一次的数字](/solution/0100-0199/0136.Single%20Number/README.md)
130132
- [只出现一次的数字 II](/solution/0100-0199/0137.Single%20Number%20II/README.md)
131133
- [只出现一次的数字 III](/solution/0200-0299/0260.Single%20Number%20III/README.md)

README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
122122

123123
### Math
124124

125+
- [Integer to Roman](/solution/0000-0099/0012.Integer%20to%20Roman/README_EN.md)
126+
- [Roman to Integer](/solution/0000-0099/0013.Roman%20to%20Integer/README_EN.md)
125127
- [Single Number](/solution/0100-0199/0136.Single%20Number/README_EN.md)
126128
- [Single Number II](/solution/0100-0199/0137.Single%20Number%20II/README_EN.md)
127129
- [Single Number III](/solution/0200-0299/0260.Single%20Number%20III/README_EN.md)

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

+55-2
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,68 @@ M 1000</pre>
6767
<!-- 这里可写当前语言的特殊实现逻辑 -->
6868

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

7398
### **Java**
7499

75100
<!-- 这里可写当前语言的特殊实现逻辑 -->
76101

77102
```java
78-
103+
class Solution {
104+
public int romanToInt(String s) {
105+
Map<String, Integer> nums = new HashMap<>();
106+
nums.put("M", 1000);
107+
nums.put("CM", 900);
108+
nums.put("D", 500);
109+
nums.put("CD", 400);
110+
nums.put("C", 100);
111+
nums.put("XC", 90);
112+
nums.put("L", 50);
113+
nums.put("XL", 40);
114+
nums.put("X", 10);
115+
nums.put("IX", 9);
116+
nums.put("V", 5);
117+
nums.put("IV", 4);
118+
nums.put("I", 1);
119+
int res = 0;
120+
for (int i = 0; i < s.length();) {
121+
if (i + 1 < s.length() && nums.get(s.substring(i, i + 2)) != null) {
122+
res += nums.get(s.substring(i, i + 2));
123+
i += 2;
124+
} else {
125+
res += nums.get(s.substring(i, i + 1));
126+
i += 1;
127+
}
128+
}
129+
return res;
130+
}
131+
}
79132
```
80133

81134
### **...**

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

+55-2
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,66 @@ M 1000</pre>
8989
### **Python3**
9090

9191
```python
92-
92+
class Solution:
93+
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
114+
else:
115+
res += nums[s[i: i + 1]]
116+
i += 1
117+
return res
93118
```
94119

95120
### **Java**
96121

97122
```java
98-
123+
class Solution {
124+
public int romanToInt(String s) {
125+
Map<String, Integer> nums = new HashMap<>();
126+
nums.put("M", 1000);
127+
nums.put("CM", 900);
128+
nums.put("D", 500);
129+
nums.put("CD", 400);
130+
nums.put("C", 100);
131+
nums.put("XC", 90);
132+
nums.put("L", 50);
133+
nums.put("XL", 40);
134+
nums.put("X", 10);
135+
nums.put("IX", 9);
136+
nums.put("V", 5);
137+
nums.put("IV", 4);
138+
nums.put("I", 1);
139+
int res = 0;
140+
for (int i = 0; i < s.length();) {
141+
if (i + 1 < s.length() && nums.get(s.substring(i, i + 2)) != null) {
142+
res += nums.get(s.substring(i, i + 2));
143+
i += 2;
144+
} else {
145+
res += nums.get(s.substring(i, i + 1));
146+
i += 1;
147+
}
148+
}
149+
return res;
150+
}
151+
}
99152
```
100153

101154
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
class Solution {
22
public int romanToInt(String s) {
3-
if (s == null || s.isEmpty()) {
4-
return 0;
5-
}
6-
String str = "IVXLCDM";
7-
int[] num = {1, 5, 10, 50, 100, 500, 1000};
8-
int ans = 0;
9-
int i = 0;
10-
int pre = 999, cur = 0;
11-
while (i < s.length()) {
12-
cur = str.indexOf(s.charAt(i++));
13-
if (pre < cur) {
14-
ans = ans + num[cur] - 2 * num[pre];
3+
Map<String, Integer> nums = new HashMap<>();
4+
nums.put("M", 1000);
5+
nums.put("CM", 900);
6+
nums.put("D", 500);
7+
nums.put("CD", 400);
8+
nums.put("C", 100);
9+
nums.put("XC", 90);
10+
nums.put("L", 50);
11+
nums.put("XL", 40);
12+
nums.put("X", 10);
13+
nums.put("IX", 9);
14+
nums.put("V", 5);
15+
nums.put("IV", 4);
16+
nums.put("I", 1);
17+
int res = 0;
18+
for (int i = 0; i < s.length();) {
19+
if (i + 1 < s.length() && nums.get(s.substring(i, i + 2)) != null) {
20+
res += nums.get(s.substring(i, i + 2));
21+
i += 2;
1522
} else {
16-
ans += num[cur];
23+
res += nums.get(s.substring(i, i + 1));
24+
i += 1;
1725
}
18-
pre = cur;
1926
}
20-
return ans;
27+
return res;
2128
}
2229
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
class Solution:
2-
def romanToInt(self, s):
3-
"""
4-
:type s: str
5-
:rtype: int
6-
"""
7-
r2i={'M':1000,'CM':900,'D':500,'CD':400,'C':100,'XC':90,'L':50,'CL':40,'X':10,'IX':9,'V':5,'IV':4,'I':1}
8-
i=0
9-
result=0
10-
while 1:
11-
if i<=(len(s)-1):
12-
try:
13-
if r2i[s[i:(i+2)]]:
14-
result += r2i[s[i:(i+2)]]
15-
i=i+2
16-
except:
17-
result += r2i[s[i]]
18-
i=i+1
2+
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
1923
else:
20-
break
21-
return result
24+
res += nums[s[i: i + 1]]
25+
i += 1
26+
return res

solution/0000-0099/0074.Search a 2D Matrix/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Solution {
7474
int m = matrix.length, n = matrix[0].length;
7575
int l = 0, h = m * n - 1;
7676
while (l <= h) {
77-
int mid = l + ((h - l) >> 1);
77+
int mid = (l + h) >>> 1;
7878
int x = mid / n, y = mid % n;
7979
if (matrix[x][y] == target) return true;
8080
if (matrix[x][y] < target) l = mid + 1;

solution/0000-0099/0074.Search a 2D Matrix/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class Solution {
8484
int m = matrix.length, n = matrix[0].length;
8585
int l = 0, h = m * n - 1;
8686
while (l <= h) {
87-
int mid = l + ((h - l) >> 1);
87+
int mid = (l + h) >>> 1;
8888
int x = mid / n, y = mid % n;
8989
if (matrix[x][y] == target) return true;
9090
if (matrix[x][y] < target) l = mid + 1;

solution/0000-0099/0074.Search a 2D Matrix/Solution.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ public boolean searchMatrix(int[][] matrix, int target) {
33
int m = matrix.length, n = matrix[0].length;
44
int l = 0, h = m * n - 1;
55
while (l <= h) {
6-
int mid = l + ((h - l) >> 1);
6+
int mid = (l + h) >>> 1;
77
int x = mid / n, y = mid % n;
88
if (matrix[x][y] == target) return true;
99
if (matrix[x][y] < target) l = mid + 1;

solution/0000-0099/0074.Search a 2D Matrix/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
33
m, n = len(matrix), len(matrix[0])
44
l, h = 0, m * n - 1
55
while l <= h:
6-
mid = l + ((h - l) >> 1)
6+
mid = (l + h) >> 1
77
x, y = divmod(mid, n)
88
if matrix[x][y] == target:
99
return True

0 commit comments

Comments
 (0)