Swift algorithm practice

12. Integer to Roman

2019-01-31  本文已影响1人  d1497e8e780a

Swift 4.2 没什么好解法 这样写比较容易看懂

class Solution {
     func intToRoman(_ num: Int) -> String {
        var M = ["","M","MM","MMM"]
        var C = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"]
        var X = ["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"]
        var I = ["","I","II","III","IV","V","VI","VII","VIII","IX"]
        let s:String = M[num/1000] + C[(num%1000)/100] + X[(num%100)/10] + I[num%10]
        return s
    }
}
上一篇 下一篇

猜你喜欢

热点阅读