-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_RomanToInteger.swift
More file actions
65 lines (65 loc) · 1.95 KB
/
13_RomanToInteger.swift
File metadata and controls
65 lines (65 loc) · 1.95 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class Solution {
func romanToInt(_ s: String) -> Int {
var num = 0
var lastRoman: Character = "/"
var lastNum = 0
for (i, c) in s.enumerated() {
switch c {
case "M":
if lastRoman=="C" {
num -= lastNum
lastNum = 900
} else {
lastNum = 1000
}
lastRoman = "/"
case "D":
if lastRoman=="C" {
num -= lastNum
lastNum = 400
} else {
lastNum = 500
}
lastRoman = "/"
case "C":
if lastRoman=="X" {
num -= lastNum
lastNum = 90
} else {
lastNum = 100
}
lastRoman = "/"
case "L":
if lastRoman=="X" {
num -= lastNum
lastNum = 40
} else {
lastNum = 50
}
lastRoman = "/"
case "X":
if lastRoman=="I" {
num -= lastNum
lastNum = 9
} else {
lastNum = 10
}
lastRoman = "/"
case "V":
if lastRoman=="I" {
num -= lastNum
lastNum = 4
} else {
lastNum = 5
}
lastRoman = "/"
default:
lastNum = 1
lastRoman = c
}
lastRoman = c
num += lastNum
}
return num
}
}