Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- func romanToInt(_ s: String) -> Int {
- let roman: [Character : Int] = [
- "I" : 1,
- "V" : 5,
- "X" : 10,
- "L" : 50,
- "C" : 100,
- "D" : 500,
- "M" : 1000
- ]
- var old: Int = 0
- var result: Int = 0
- for character in s {
- let newInt = roman[character] ?? 0
- result += newInt > old ? newInt - old * 2 : newInt
- old = newInt
- }
- return result
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement