leetcode -- Integer to Roman -- 重点

来源:互联网 发布:office of mac免费版本 编辑:程序博客网 时间:2024/06/08 23:39

https://leetcode.com/problems/integer-to-roman/

http://www.cnblogs.com/zuoyuan/p/3779581.html

3个1,4,5,9,再加一个1000.这样来记罗马数字

看code:

class Solution(object):    def intToRoman(self, num):        """        :type num: int        :rtype: str        """        values = [ 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 ]        numerals = [ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" ]        list = ''        for i in range(0, len(values)):#这里有点greedy算法的意思,如果把num看做杯子的容积,那么每次都选择尽量能填满这个杯子的水。自己举例可以理解            while num >= values[i]:                num -= values[i]                list += numerals[i]        return list
0 0