Skip to content

Commit c3251a4

Browse files
committed
Update solution 013
1 parent df8d7aa commit c3251a4

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Complete solutions to Leetcode problems, updated daily.
1414
|---|---|---|
1515
| 001 | [Two Sum](https://github.com/yanglbme/leetcode/tree/master/solution/001.Two%20Sum) | `Array`, `Hash Table` |
1616
| 007 | [Reverse Integer](https://github.com/yanglbme/leetcode/tree/master/solution/007.Reverse%20Integer) | `Math` |
17+
| 013 | [Roman to Integer](https://github.com/yanglbme/leetcode/tree/master/solution/013.Roman%20to%20Integer) | `Math`, `String` |
1718
| 021 | [Merge Two Sorted Lists](https://github.com/yanglbme/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
1819
| 083 | [Remove Duplicates from Sorted List](https://github.com/yanglbme/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
1920
| 198 | [House Robber](https://github.com/yanglbme/leetcode/tree/master/solution/198.House%20Robber) | `Dynamic Programming` |
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
## 罗马数字转整数
2+
### 题目描述
3+
4+
罗马数字包含以下七种字符:`I``V``X``L``C``D``M`
5+
6+
```
7+
字符 数值
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
```
16+
17+
例如, 罗马数字 2 写做 `II` ,即为两个并列的 1。12 写做 `XII` ,即为 `X` + `II` 。 27 写做 `XXVII`, 即为 `XX` + `V` + `II`
18+
19+
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 `IIII`,而是 `IV`。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 `IX`。这个特殊的规则只适用于以下六种情况:
20+
21+
- `I` 可以放在 `V` (5) 和 `X` (10) 的左边,来表示 4 和 9。
22+
- `X` 可以放在 `L` (50) 和 `C` (100) 的左边,来表示 40 和 90。
23+
- `C` 可以放在 `D` (500) 和 `M` (1000) 的左边,来表示 400 和 900。
24+
25+
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
26+
27+
示例 1:
28+
```
29+
输入: "III"
30+
输出: 3
31+
```
32+
33+
示例 2:
34+
```
35+
输入: "IV"
36+
输出: 4
37+
```
38+
39+
示例 3:
40+
```
41+
输入: "IX"
42+
输出: 9
43+
```
44+
45+
示例 4:
46+
```
47+
输入: "LVIII"
48+
输出: 58
49+
解释: C = 100, L = 50, XXX = 30, III = 3.
50+
```
51+
52+
示例 5:
53+
```
54+
输入: "MCMXCIV"
55+
输出: 1994
56+
解释: M = 1000, CM = 900, XC = 90, IV = 4.
57+
```
58+
59+
### 解法
60+
用 map 存储字符串及对应的值,遍历 `s`,若 s[i, i + 1] 在 map 中,累加对应的值,i 向右移动两格;否则累加 s[i],i 向右移动一格。
61+
62+
```java
63+
class Solution {
64+
public int romanToInt(String s) {
65+
Map<String, Integer> map = new HashMap<String, Integer>(13) {{
66+
put("I", 1);
67+
put("V", 5);
68+
put("X", 10);
69+
put("L", 50);
70+
put("C", 100);
71+
put("D", 500);
72+
put("M", 1000);
73+
put("IV", 4);
74+
put("IX", 9);
75+
put("XL", 40);
76+
put("XC", 90);
77+
put("CD", 400);
78+
put("CM", 900);
79+
}};
80+
81+
int res = 0;
82+
int len = s.length();
83+
for (int i = 0; i < len; ++i) {
84+
if (i != len - 1 && map.get(s.substring(i, i + 2)) != null) {
85+
res += map.get(s.substring(i, i + 2));
86+
++i;
87+
continue;
88+
}
89+
res += map.get(s.substring(i, i + 1));
90+
}
91+
return res;
92+
93+
}
94+
}
95+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public int romanToInt(String s) {
3+
Map<String, Integer> map = new HashMap<String, Integer>(13) {
4+
{
5+
put("I", 1);
6+
put("V", 5);
7+
put("X", 10);
8+
put("L", 50);
9+
put("C", 100);
10+
put("D", 500);
11+
put("M", 1000);
12+
put("IV", 4);
13+
put("IX", 9);
14+
put("XL", 40);
15+
put("XC", 90);
16+
put("CD", 400);
17+
put("CM", 900);
18+
}
19+
};
20+
21+
int res = 0;
22+
int len = s.length();
23+
for (int i = 0; i < len; ++i) {
24+
if (i != len - 1 && map.get(s.substring(i, i + 2)) != null) {
25+
res += map.get(s.substring(i, i + 2));
26+
++i;
27+
continue;
28+
}
29+
res += map.get(s.substring(i, i + 1));
30+
}
31+
return res;
32+
33+
}
34+
}

0 commit comments

Comments
 (0)