Skip to content

Commit ad0e216

Browse files
committed
Add roman-to-integer
1 parent 17d513f commit ad0e216

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
|0102|[二叉树的层次遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/submissions/)|[go](./tree/102.levelOrder.go)|M|
3030
|0103|[二叉树的锯齿形层次遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/submissions/)|[go](./tree/103.zigzagLevelOrder.go)|M|
3131
|0104|[二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)|[go](./tree/104.maxDepth.go)|S|
32+
|0113|[罗马数字转整数](https://leetcode-cn.com/problems/roman-to-integer/)|[go](./mystring/113.romanToInt.go)|S|
3233
|0118|[杨辉三角](https://leetcode-cn.com/problems/pascals-triangle/)|[go](./myarray/118.generate.go)|S|
3334
|0121|[买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)|[go](./myarray/121.maxProfit.go)|S|
3435
|0136|[只出现一次的数字](https://leetcode-cn.com/problems/single-number/comments/)|[go](./mybit/136.singleNumber.go)|S|

algorithms/mystring/13.romanToInt.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package mystring
2+
3+
// 思路:建立一个Map来映射符号和值,然后对字符串从后向前遍历,保留上一个字符串
4+
// 如果当前字符代表的值大于等于上一个(右边),就加上该值;
5+
// 如果当前字符代表的值小于上一个(右边),则减去该值。
6+
// romanToInt1 returns int
7+
func romanToInt1(s string) int {
8+
m := map[byte]int{
9+
'I': 1,
10+
'V': 5,
11+
'X': 10,
12+
'L': 50,
13+
'C': 100,
14+
'D': 500,
15+
'M': 1000,
16+
}
17+
18+
var res, last int
19+
for i := len(s) - 1; i >= 0; i-- {
20+
curr := m[s[i]]
21+
flag := 1
22+
// 小数在大数的左边,要减去小数
23+
if curr < last {
24+
flag = -1
25+
}
26+
27+
res += flag * curr
28+
last = curr
29+
}
30+
31+
return res
32+
}
33+
34+
// 思路:将所有的组合可能性列出并添加到哈希表中,
35+
// 然后对字符串进行遍历,先判断两个字符的组合在哈希表中是否存在,存在则将值取出加到结果 res 中,并向后移2个字符;
36+
// 不存在则将判断当前 1 个字符是否存在,存在则将值取出加到结果 res 中,并向后移 1 个字符。
37+
// romanToInt2 returns int
38+
func romanToInt2(s string) int {
39+
m := map[string]int{
40+
"I": 1,
41+
"V": 5,
42+
"X": 10,
43+
"L": 50,
44+
"C": 100,
45+
"D": 500,
46+
"M": 1000,
47+
"IV": 4,
48+
"IX": 9,
49+
"XL": 40,
50+
"XC": 90,
51+
"CD": 400,
52+
"CM": 900,
53+
}
54+
55+
var res int
56+
for i := 0; i < len(s); {
57+
if i+1 < len(s) && containsKey(m, string(s[i:i+2])) {
58+
res += m[string(s[i:i+2])]
59+
i += 2
60+
} else if containsKey(m, string(s[i:i+1])) {
61+
res += m[string(s[i:i+1])]
62+
i++
63+
}
64+
}
65+
66+
return res
67+
}
68+
69+
// containsKey return true if m contains k
70+
func containsKey(m map[string]int, k string) bool {
71+
if _, ok := m[k]; ok {
72+
return true
73+
}
74+
return false
75+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package mystring
2+
3+
import (
4+
"Leetcode/algorithms/kit"
5+
"testing"
6+
)
7+
8+
// run: go test -v 13.*
9+
func Test_romanToInt(t *testing.T) {
10+
cases := []kit.CaseEntry{
11+
{
12+
Name: "x1",
13+
Input: "III",
14+
Expected: 3,
15+
},
16+
{
17+
Name: "x2",
18+
Input: "IV",
19+
Expected: 4,
20+
},
21+
{
22+
Name: "x3",
23+
Input: "IX",
24+
Expected: 9,
25+
},
26+
{
27+
Name: "x4",
28+
Input: "LVIII",
29+
Expected: 58,
30+
},
31+
{
32+
Name: "x5",
33+
Input: "MCMXCIV",
34+
Expected: 1994,
35+
},
36+
}
37+
38+
for _, c := range cases {
39+
t.Run(c.Name, func(t *testing.T) {
40+
if output := romanToInt1(c.Input.(string)); output != c.Expected.(int) {
41+
t.Errorf("romanToInt1(%s)=%d, expected=%d", c.Input, output, c.Expected)
42+
}
43+
44+
if output := romanToInt2(c.Input.(string)); output != c.Expected.(int) {
45+
t.Errorf("romanToInt2(%s)=%d, expected=%d", c.Input, output, c.Expected)
46+
}
47+
})
48+
}
49+
}

0 commit comments

Comments
 (0)