Skip to content
Closed
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
35 changes: 35 additions & 0 deletions Roman to Integer - Leetcode 13/Roman to integer - Leetcode 13.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package solution

func romanToInt(s string) int {
val := 0
prevVal := 1
for i := len(s) - 1; i >= 0; i-- {
sVal := symbolVal(s[i])
if prevVal > sVal {
val -= sVal
} else {
val += sVal
}
prevVal = sVal
}
return val
}

func symbolVal(symbol byte) int {
switch symbol {
case 'M':
return 1000
case 'D':
return 500
case 'C':
return 100
case 'L':
return 50
case 'X':
return 10
case 'V':
return 5
default:
return 1
}
}