Advertisement
azamat_tajiyev

13. Roman to Integer

Apr 24th, 2024
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.53 KB | None | 0 0
  1. class Solution {
  2.     func romanToInt(_ s: String) -> Int {
  3.         let roman: [Character : Int] = [
  4.             "I" : 1,
  5.             "V" : 5,
  6.             "X" : 10,
  7.             "L" : 50,
  8.             "C" : 100,
  9.             "D" : 500,
  10.             "M" : 1000
  11.         ]
  12.         var old: Int = 0
  13.         var result: Int = 0
  14.         for character in s {
  15.             let newInt = roman[character] ?? 0
  16.             result += newInt > old ? newInt - old * 2  : newInt
  17.             old = newInt
  18.         }
  19.         return result
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement