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

13. Roman to Integer #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from roman-to-integer
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ continually updating 😃.
### Math
* [7. Reverse Integer](src/0007_reverse_integer/reverse_integer.go)
* [9. Palindrome Number](src/0009_palindrome_number/palindrome_number.go)
* [13. Roman to Integer](src/0013_roman_to_integer/roman_to_integer.go)   *`string`*

<details>
</details>
29 changes: 29 additions & 0 deletions src/0013_roman_to_integer/roman_to_integer.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
13. Roman to Integer
https://leetcode.com/problems/roman-to-integer/
*/
// time: 2018年12月31日

package rti

// time complexity: O(n)
// space complexity: O(1)
func romanToInt(s string) int {
if 0 == len(s) {
return 0
}

digits := map[byte]int{'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
sum := digits[s[len(s)-1]]

for i := len(s) - 1; i > 0; i-- {
cur := digits[s[i]]
pre := digits[s[i-1]]
if cur > pre {
sum -= pre
} else {
sum += pre
}
}
return sum
}
21 changes: 21 additions & 0 deletions src/0013_roman_to_integer/roman_to_integer_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package rti

import "testing"

func TestRomanToInt(t *testing.T) {
testCases := []string{
"III",
"IV",
"IX",
"LVIII",
"MCMXCIV",
"",
}
expected := []int{3, 4, 9, 58, 1994, 0}

for index, s := range testCases {
if res := romanToInt(s); res != expected[index] {
t.Errorf("expected %d, got %d", expected[index], res)
}
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
|0007|[7. Reverse Integer](0007_reverse_integer/reverse_integer.go)|Easy|*`math`*|
|0009|[9. Palindrome Number](0009_palindrome_number/palindrome_number.go)|Easy|*`math`*|
|0011|[11. Container With Most Water](0011_container_with_most_water/container_with_most_water.go)|Medium|*`array;`* *`double index`*|
|0013|[13. Roman to Integer](0013_roman_to_integer/roman_to_integer.go)|Easy|*`math`*|
|0017|[Letter Combinations of a Phone Number](0017_letter_combination_of_a_phone_number/letter_combination_of_phone_number.go)|Medium|*`tree`*|
|0020|[Valid Parentheses](0020_valid_parentheses/valid_parentheses.go)|Easy|*`string;`* *`stack`*|
|0021|[Merge Two Sorted Lists](0021_merge_two_sorted_lists/mergeTwoLists.go)|Easy|*`linked list`*|
Expand Down

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