Skip to content

Commit 9f7824e

Browse files
xiyouxiyou
authored andcommitted
update 0013
1 parent a288c07 commit 9f7824e

File tree

3 files changed

+51
-78
lines changed

3 files changed

+51
-78
lines changed
Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,51 @@
1-
package problem0013
1+
pub struct Solution {}
22

3-
func romanToInt(s string) int {
4-
res := 0
5-
m := map[byte]int{
6-
'I': 1,
7-
'V': 5,
8-
'X': 10,
9-
'L': 50,
10-
'C': 100,
11-
'D': 500,
12-
'M': 1000,
13-
}
3+
fn transform(s: &str) -> i32 {
4+
match s {
5+
"I" => 1,
6+
"V" => 5,
7+
"X" => 10,
8+
"L" => 50,
9+
"C" => 100,
10+
"D" => 500,
11+
"M" => 1000,
12+
"IV" => 4,
13+
"IX" => 9,
14+
"XL" => 40,
15+
"XC" => 90,
16+
"CD" => 400,
17+
"CM" => 900,
18+
_ => 0,
19+
}
20+
}
21+
22+
impl Solution {
23+
pub fn roman_to_int(s: String) -> i32 {
24+
let s_length = s.len();
25+
let mut i = 0;
26+
let mut result = 0;
1427

15-
last := 0
16-
for i := len(s) - 1; i >= 0; i-- {
17-
temp := m[s[i]]
28+
while i < s_length {
29+
let ostr = s.chars().nth(i).unwrap().to_string();
30+
let mut nstr = String::from("");
1831

19-
sign := 1
20-
if temp < last {
21-
//小数在大数的左边,要减去小数
22-
sign = -1
23-
}
32+
i += 1;
33+
if i < s_length {
34+
let nchar = s.chars().nth(i).unwrap();
35+
nstr.push_str(&ostr);
36+
nstr.push(nchar);
2437

25-
res += sign * temp
38+
let val = transform(&nstr);
39+
if val != 0 {
40+
result += val;
41+
i += 1;
42+
continue;
43+
}
44+
};
2645

27-
last = temp
28-
}
46+
result += transform(&ostr);
47+
}
2948

30-
return res
49+
return result;
50+
}
3151
}
Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,7 @@
1-
package problem0013
1+
mod roman_to_integer;
2+
use roman_to_integer::Solution;
23

3-
import (
4-
"testing"
5-
6-
"github.com/stretchr/testify/assert"
7-
)
8-
9-
type question struct {
10-
para
11-
ans
12-
}
13-
14-
// para 是参数
15-
// one 代表第一个参数
16-
type para struct {
17-
one string
18-
}
19-
20-
// ans 是答案
21-
// one 代表第一个答案
22-
type ans struct {
23-
one int
24-
}
25-
26-
func Test_Problem0013(t *testing.T) {
27-
ast := assert.New(t)
28-
29-
qs := []question{
30-
question{
31-
para{"XXXIX"},
32-
ans{39},
33-
},
34-
question{
35-
para{"MDCCCLXXXVIII"},
36-
ans{1888},
37-
},
38-
question{
39-
para{"MCMLXXVI"},
40-
ans{1976},
41-
},
42-
question{
43-
para{"MMMCMXCIX"},
44-
ans{3999},
45-
},
46-
47-
// 如需多个测试,可以复制上方元素。
48-
}
49-
50-
for _, q := range qs {
51-
a, p := q.ans, q.para
52-
53-
ast.Equal(a.one, romanToInt(p.one), "输入:%v", p)
54-
}
4+
fn main() {
5+
let results = Solution::roman_to_int("MCMXCIV".to_owned());
6+
println!("{:?}", results);
557
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ leetcode in rust lang.
1616
|1|[Two Sum](./Algorithms/0001.two_sum)|39%|Easy||
1717
|2|[Add Two Numbers](./Algorithms/0002.add_two_numbers)|29%|Medium||
1818
|3|[Longest Substring Without Repeating Characters](./Algorithms/0003.longest_substring_without_repeating_characters)|25%|Medium||
19-
|4|[Median of Two Sorted Arrays](./Algorithms/0004.median_of_two_sorted_arrays)|24%|Hard||
19+
|4|[Median of Two Sorted Arrays](./Algorithms/0004.median_of_two_sorted_arrays)|24%|Hard||
20+
|13|[Roman to Integer](./Algorithms/0013.roman_to_integer)|24%|Easy||

0 commit comments

Comments
 (0)