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

Commit 703e17b

Browse files
authored
13 solved. (#28)
1 parent 2f4d8fa commit 703e17b

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ continually updating 😃.
8888
### Math
8989
* [7. Reverse Integer](src/0007_reverse_integer/reverse_integer.go)
9090
* [9. Palindrome Number](src/0009_palindrome_number/palindrome_number.go)
91+
* [13. Roman to Integer](src/0013_roman_to_integer/roman_to_integer.go)   *`string`*
9192

9293
<details>
9394
</details>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
13. Roman to Integer
3+
https://leetcode.com/problems/roman-to-integer/
4+
*/
5+
// time: 2018年12月31日
6+
7+
package rti
8+
9+
// time complexity: O(n)
10+
// space complexity: O(1)
11+
func romanToInt(s string) int {
12+
if 0 == len(s) {
13+
return 0
14+
}
15+
16+
digits := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
17+
sum := digits[s[len(s)-1]]
18+
19+
for i := len(s) - 1; i > 0; i-- {
20+
cur := digits[s[i]]
21+
pre := digits[s[i-1]]
22+
if cur > pre {
23+
sum -= pre
24+
} else {
25+
sum += pre
26+
}
27+
}
28+
return sum
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package rti
2+
3+
import "testing"
4+
5+
func TestRomanToInt(t *testing.T) {
6+
testCases := []string{
7+
"III",
8+
"IV",
9+
"IX",
10+
"LVIII",
11+
"MCMXCIV",
12+
"",
13+
}
14+
expected := []int{3, 4, 9, 58, 1994, 0}
15+
16+
for index, s := range testCases {
17+
if res := romanToInt(s); res != expected[index] {
18+
t.Errorf("expected %d, got %d", expected[index], res)
19+
}
20+
}
21+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
|0007|[7. Reverse Integer](0007_reverse_integer/reverse_integer.go)|Easy|*`math`*|
1010
|0009|[9. Palindrome Number](0009_palindrome_number/palindrome_number.go)|Easy|*`math`*|
1111
|0011|[11. Container With Most Water](0011_container_with_most_water/container_with_most_water.go)|Medium|*`array;`* *`double index`*|
12+
|0013|[13. Roman to Integer](0013_roman_to_integer/roman_to_integer.go)|Easy|*`math`*|
1213
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
1314
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
1415
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /