13. 罗马数字转整数

罗马数字转整数

class Solution {
    func romanToInt(_ s: String) -> Int {
        var result = 0
        let length = s.count
        for i in (0...length-1).reversed(){
        let c : Character = s[i]
        switch c{
        case "I":
            result += result >= 5 ? -1 : 1
         case "V":
            result += 5
            case "X":
                result += 10*(result >= 50 ? -1 : 1)
            case "L":
                result += 50
            case "C":
                result += 100*(result >= 500 ? -1 : 1)
            case "D":
                result += 500
            case "M":
                result += 1000
            default:
                break

}

}
        return result
        
    }
}
private extension String {
    subscript (index: Int) -> Character {
        return self[self.index(self.startIndex, offsetBy: index)]
    }
}

你可能感兴趣的:(13. 罗马数字转整数)