File tree 3 files changed +36
-0
lines changed
3 files changed +36
-0
lines changed Original file line number Diff line number Diff line change @@ -13,5 +13,6 @@ leetcode for golang
13
13
#### [ 9. palindrome number] ( https://github.com/hitzzc/go-leetcode/tree/master/palindrome_number )
14
14
#### [ 11. container with most water] ( https://github.com/hitzzc/go-leetcode/tree/master/container_with_most_water )
15
15
#### [ 12. integer to roman] ( https://github.com/hitzzc/go-leetcode/tree/master/integer_to_roman )
16
+ #### [ 12. roman to integer] ( https://github.com/hitzzc/go-leetcode/tree/master/roman_to_integer )
16
17
17
18
Original file line number Diff line number Diff line change
1
+ package roman_to_integer
2
+
3
+ func romanToInt (s string ) int {
4
+ m := map [rune ]int {
5
+ rune ('I' ): 1 ,
6
+ rune ('V' ): 5 ,
7
+ rune ('X' ): 10 ,
8
+ rune ('L' ): 50 ,
9
+ rune ('C' ): 100 ,
10
+ rune ('D' ): 500 ,
11
+ rune ('M' ): 1000 ,
12
+ }
13
+ runes := []rune (s )
14
+ if len (runes ) == 1 {
15
+ return m [runes [0 ]]
16
+ }
17
+ ret := m [runes [0 ]]
18
+ for i := 1 ; i < len (runes ); i ++ {
19
+ if m [runes [i - 1 ]] < m [runes [i ]] {
20
+ ret += m [runes [i ]] - 2 * m [runes [i - 1 ]]
21
+ } else {
22
+ ret += m [runes [i ]]
23
+ }
24
+ }
25
+ return ret
26
+ }
Original file line number Diff line number Diff line change
1
+ package roman_to_integer
2
+
3
+ import (
4
+ "testing"
5
+ )
6
+
7
+ func TestRomanToInt (t * testing.T ) {
8
+
9
+ }
You can’t perform that action at this time.
0 commit comments