add new solutions
This commit is contained in:
parent
e1b702657c
commit
59b71480d4
11 changed files with 343 additions and 0 deletions
37
solutions/0/q13/solution.go
Normal file
37
solutions/0/q13/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q13
|
||||
|
||||
func lookup(b byte) int {
|
||||
switch b {
|
||||
case 'I':
|
||||
return 1
|
||||
case 'V':
|
||||
return 5
|
||||
case 'X':
|
||||
return 10
|
||||
case 'L':
|
||||
return 50
|
||||
case 'C':
|
||||
return 100
|
||||
case 'D':
|
||||
return 500
|
||||
case 'M':
|
||||
return 1000
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func romanToInt(s string) int {
|
||||
num := 0
|
||||
prev := 0
|
||||
for i := range len(s) {
|
||||
cur := lookup(s[i])
|
||||
num += cur
|
||||
if prev < cur {
|
||||
num -= prev + prev
|
||||
}
|
||||
prev = cur
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
var _ = romanToInt
|
||||
Loading…
Add table
Add a link
Reference in a new issue