-
Notifications
You must be signed in to change notification settings - Fork 1
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
ChobobDev
wants to merge
1
commit into
main
Choose a base branch
from
chobobdev-210608
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} | ||
|
||
// 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
들여쓰기랑 줄넘김이 개선되면 좋을 것 같아요 👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이런 실수를 했을리 없어... 에디터 문제 인거죠...?