Skip to content
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

20210608-13.Roman to integer solution #40

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions chobobdev/13/13.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

// Symbol Value
// I 1
// V 5
// X 10
// L 50
// C 100
// D 500
// M 1000
// For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

// Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

// I can be placed before V (5) and X (10) to make 4 and 9.
// X can be placed before L (50) and C (100) to make 40 and 90.
// C can be placed before D (500) and M (1000) to make 400 and 900.
// Given a roman numeral, convert it to an integer.

// Example 1:

// Input: s = "III"
// Output: 3
// Example 2:

// Input: s = "IV"
// Output: 4
// Example 3:

// Input: s = "IX"
// Output: 9
// Example 4:

// Input: s = "LVIII"
// Output: 58
// Explanation: L = 50, V= 5, III = 3.
// Example 5:

// Input: s = "MCMXCIV"
// Output: 1994
// Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

// Constraints:

// 1 <= s.length <= 15
// s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
// It is guaranteed that s is a valid roman numeral in the range [1, 3999].

var roman = map[string]int{
"I": 1,
"V": 5,
"X": 10,
"L": 50,
"C": 100,
"D": 500,
"M": 1000,
}

func romanToInt(s string) int {
if len(s) == 0 {
return 0
}
currentnum, prevint, total := 0, 0, 0
for i := 0; i < len(s); i++ {
char := s[len(s)-(i+1) : len(s)-i]
currentnum = roman[char]
if currentnum < prevint {
total = total - currentnum
} else {
total = total + currentnum
}
prevint = currentnum
}
return total

}
Comment on lines +59 to +76
Copy link
Member

@ForestLee0513 ForestLee0513 Jun 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

들여쓰기랑 줄넘김이 개선되면 좋을 것 같아요 👀

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 실수를 했을리 없어... 에디터 문제 인거죠...?


// Runtime: 12 ms, faster than 31.15% of Go online submissions for Roman to Integer.
// Memory Usage: 3.3 MB, less than 19.78% of Go online submissions for Roman to Integer.
13 changes: 7 additions & 6 deletions chobobdev/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ Go lang을 활용해 많은 알고리즘 해결
| # | 시도 날짜 | 문제 이름 | 언어 | 링크 |
| :--: | :--------: | :-------: | :--: |:--------------------------: |
| 1 | 2021-05-20 | 27. Remove Element| Go |[:link:](27.go) |
| 2 | 2021-05-23 | 58 . Length of the last word | Go |[:link:](58.md)|
| 3 | 2021-05-24 | 7. Reverse Integer | Go,JS |[:link:](7.md)|
| 5 | 2021-05-20 | 26.Remove Duplicates from Sorted Array| Go |[:link:](26.md) |
| 6 | 2021-05-29 | 9. Palindrome Number | Go | [:link:](9.md) |
| 7 | 2021-05-31 |69. Sqrt(x) | Go | [:link:](69.md) |
| 8 | 2021-05-31 |796. Rotate String | Go | [:link:](796.md) |
| 2 | 2021-05-23 | 58 . Length of the last word | Go |[:link:](58.go)|
| 3 | 2021-05-24 | 7. Reverse Integer | Go,JS |[:link:](7.go)|
| 5 | 2021-05-20 | 26.Remove Duplicates from Sorted Array| Go |[:link:](26.go) |
| 6 | 2021-05-29 | 9. Palindrome Number | Go | [:link:](9.go) |
| 7 | 2021-05-31 |69. Sqrt(x) | Go | [:link:](69.go) |
| 8 | 2021-05-31 |796. Rotate String | Go | [:link:](796.go) |
| 9 | 2021-06-08 | 13. Roman to Integer | Go | [:link:](13.go) |


<div align="center">
Expand Down