Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc54953d

Browse files
committed
update 12 problem solution
1 parent9457739 commitc54953d

File tree

3 files changed

+91
-77
lines changed

3 files changed

+91
-77
lines changed

‎src/0013.Roman-to-Integer/README.md

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,23 @@ Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
7878

7979
那么我们可以利用 map 来完成罗马数字的 7 个数字符号:I、V、X、L、C、D、M 和整数的映射关系,然后根据上面的解释来模拟完成即可。
8080

81-
```java
82-
classSolution {
83-
publicintromanToInt(Strings) {
84-
Map<Character,Integer> map=newHashMap<>();
85-
map.put('I',1);
86-
map.put('V',5);
87-
map.put('X',10);
88-
map.put('L',50);
89-
map.put('C',100);
90-
map.put('D',500);
91-
map.put('M',1000);
92-
int len= s.length();
93-
int sum= map.get(s.charAt(len-1));
94-
for (int i= len-2; i>=0;--i) {
95-
if (map.get(s.charAt(i))< map.get(s.charAt(i+1))) {
96-
sum-= map.get(s.charAt(i));
97-
}else {
98-
sum+= map.get(s.charAt(i));
99-
}
100-
}
101-
return sum;
102-
}
81+
```go
82+
funcromanToInt(sstring)int {
83+
m:=map[string]int{"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
84+
sum:= m[string(s[len(s)-1])]
85+
// 从后向前遍历
86+
// 每次和前面一位数比较
87+
fori:=len(s) -2; i >=0; i-- {
88+
if m[string(s[i])] < m[string(s[i+1])] {
89+
sum -= m[string(s[i])]
90+
}else {
91+
sum += m[string(s[i])]
92+
}
93+
}
94+
95+
return sum
10396
}
97+
10498
```
10599

106100

‎src/0013.Roman-to-Integer/Solution.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
package Solution
22

33
funcromanToInt(sstring)int {
4-
//初始化一个罗马字符的Map
5-
romanMap:=map[string]int{}
6-
romanMap["I"]=1
7-
romanMap["V"]=5
8-
romanMap["X"]=10
9-
romanMap["L"]=50
10-
romanMap["C"]=100
11-
romanMap["D"]=500
12-
romanMap["M"]=1000
13-
14-
MapIndex:=string(s[len(s)-1])
15-
sum:=romanMap[MapIndex]
4+
m:=map[string]int{"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
5+
sum:=m[string(s[len(s)-1])]
6+
// 从后向前遍历
7+
// 每次和前面一位数比较
168
fori:=len(s)-2;i>=0;i-- {
17-
//左小右大
18-
ifromanMap[string(s[i])]<romanMap[string(s[i+1])] {
19-
sum-=romanMap[string(s[i])]
20-
//左大右小
9+
ifm[string(s[i])]<m[string(s[i+1])] {
10+
sum-=m[string(s[i])]
2111
}else {
22-
sum+=romanMap[string(s[i])]
12+
sum+=m[string(s[i])]
2313
}
2414
}
2515

2616
returnsum
2717
}
18+
19+
funcromanToInt2(sstring)int {
20+
m:=map[string]int{"I":1,"V":5,"X":10,"L":50,"C":100,"D":500,"M":1000}
21+
ans:=0
22+
// 从前向后遍历
23+
// 每次默认相加再检查和前面一位数的大小
24+
// 前面 > 后面 ans = s[i-1] + s[i]
25+
// 前面 < 后面 ans = - (2 * s[i-1]) + s[i] (因为默认加了一次所以需要减2次)
26+
fori,_:=ranges {
27+
ans+=m[string(s[i])]
28+
ifi>0&&m[string(s[i])]>m[string(s[i-1])] {
29+
ans-=2*m[string(s[i-1])]
30+
}
31+
}
32+
returnans
33+
34+
}
Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,59 @@
11
package Solution
22

3-
import"testing"
3+
import (
4+
"reflect"
5+
"strconv"
6+
"testing"
7+
)
48

59
funcTestSolution(t*testing.T) {
6-
t.Run("Test-1",func(t*testing.T) {
7-
got:=romanToInt("III")
8-
want:=3
9-
ifgot!=want {
10-
t.Error("GOT:",got,"WANT:",want)
11-
}
12-
})
10+
//测试用例
11+
cases:= []struct {
12+
namestring
13+
inputsstring
14+
expectint
15+
}{
16+
{"TestCase","III",3},
17+
{"TestCase","IV",4},
18+
{"TestCase","IX",9},
19+
{"TestCase","LVIII",58},
20+
{"TestCase","MCMXCIV",1994},
21+
}
1322

14-
t.Run("Test-2",func(t*testing.T) {
15-
got:=romanToInt("IV")
16-
want:=4
17-
ifgot!=want {
18-
t.Error("GOT:",got,"WANT:",want)
19-
}
20-
})
21-
22-
t.Run("Test-3",func(t*testing.T) {
23-
got:=romanToInt("IX")
24-
want:=9
25-
ifgot!=want {
26-
t.Error("GOT:",got,"WANT:",want)
27-
}
28-
})
29-
30-
t.Run("Test-4",func(t*testing.T) {
31-
got:=romanToInt("LVIII")
32-
want:=58
33-
ifgot!=want {
34-
t.Error("GOT:",got,"WANT:",want)
35-
}
36-
})
23+
//开始测试
24+
fori,c:=rangecases {
25+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
26+
got:=romanToInt(c.inputs)
27+
if!reflect.DeepEqual(got,c.expect) {
28+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
29+
c.expect,got,c.inputs)
30+
}
31+
})
32+
}
33+
}
3734

38-
t.Run("Test-5",func(t*testing.T) {
39-
got:=romanToInt("MCMXCIV")
40-
want:=1994
41-
ifgot!=want {
42-
t.Error("GOT:",got,"WANT:",want)
43-
}
44-
})
35+
funcTestSolution2(t*testing.T) {
36+
//测试用例
37+
cases:= []struct {
38+
namestring
39+
inputsstring
40+
expectint
41+
}{
42+
{"TestCase","III",3},
43+
{"TestCase","IV",4},
44+
{"TestCase","IX",9},
45+
{"TestCase","LVIII",58},
46+
{"TestCase","MCMXCIV",1994},
47+
}
4548

49+
//开始测试
50+
fori,c:=rangecases {
51+
t.Run(c.name+" "+strconv.Itoa(i),func(t*testing.T) {
52+
got:=romanToInt2(c.inputs)
53+
if!reflect.DeepEqual(got,c.expect) {
54+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
55+
c.expect,got,c.inputs)
56+
}
57+
})
58+
}
4659
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp