美文网首页
[Swift LeetCode]12. Integer to R

[Swift LeetCode]12. Integer to R

作者: NinthDay | 来源:发表于2016-01-18 21:51 被阅读147次

题目

原题链接
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.

思路

TODO:之后添加

代码

class Solution {
    func intToRoman(num: Int) -> String {
        var str:String = ""
        var num_local:Int = num
        var symbol:[String] = ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
        var value:[Int] = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
        
        for(var i :Int = 0;num_local != 0;i++){
            while(num_local >= value[i])
            {
                num_local -= value[i]
                str += symbol[i]
            }
        }
        return str
    }
}

相关文章

网友评论

      本文标题:[Swift LeetCode]12. Integer to R

      本文链接:https://www.haomeiwen.com/subject/jnmkkttx.html