forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RomanToInteger.swift
41 lines (35 loc) · 1.01 KB
/
RomanToInteger.swift
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
/**
* Question Link: https://leetcode.com/problems/roman-to-integer/
* Primary idea: Iterate through end to start, add or minus according to different situations
* Time Complexity: O(n), Space Complexity: O(n)
*
*/
class RomanToInteger {
func romanToInt(s: String) -> Int {
let dict = initDict()
let chars = [Character](s.characters.reverse())
var res = 0
for i in 0..<chars.count {
guard let current = dict[String(chars[i])] else {
return res
}
if i > 0 && current < dict[String(chars[i - 1])] {
res -= current
} else {
res += current
}
}
return res
}
private func initDict() -> [String: Int] {
var dict = [String: Int]()
dict["I"] = 1
dict["V"] = 5
dict["X"] = 10
dict["L"] = 50
dict["C"] = 100
dict["D"] = 500
dict["M"] = 1000
return dict
}
}