-
Notifications
You must be signed in to change notification settings - Fork 1
/
RomanToInteger.kt
38 lines (37 loc) · 1.03 KB
/
RomanToInteger.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package leetcode
class RomanToInteger {
fun romanToInt(s: String): Int {
var result = s
var converted = 0
if(result.contains("CM")){
result = result.replace("CM","DCD")
}
if(result.contains("CD")){
result = result.replace("CD","CCCC")
}
if(result.contains("XC")){
result = result.replace("XC","LXL")
}
if(result.contains("XL")){
result = result.replace("XL","XXXX")
}
if(result.contains("IX")){
result = result.replace("IX","VIV")
}
if(result.contains("IV")){
result = result.replace("IV","IIII")
}
for (i in result){
when(i){
'I' -> converted++
'V' -> converted+=5
'X' -> converted+=10
'L' -> converted+=50
'C' -> converted+=100
'D' -> converted+=500
'M' -> converted+=1000
}
}
return converted
}
}