[LeetCode][Python]Integer to Roman

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘https://oj.leetcode.com/problems/integer-to-roman/Integer to Roman

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.===Comments by Dabay===先google一下罗马数字的表示:I - 1V - 5X - 10L - 50C - 100D - 500M - 1000主要问题是会有一些4,40之类的表示。比如999的时候,因为900可以表示为CM,所以需要先生成CM,数字减少900为99;而不是减少500为499.可以生成一个有序的键值表,每次减少最大的,直到剩下的数小于hash表中的最大数,把这个最大数删掉继续处理。‘‘‘

class Solution:    # @return a string    def intToRoman(self, num):        pairs = [            (1000,"M"),            (900,"CM"),            (500,"D"),            (400,"CD"),            (100,"C"),            (90,"XC"),            (50,"L"),            (40,"XL"),            (10,"X"),            (9,"IX"),            (5,"V"),            (4,"IV"),            (1,"I")        ]        res = ""        for (n, s) in pairs:            while num >= n:                res = res + s                num = num - n        return res

def main():    s = Solution()    print s.intToRoman(6)

if __name__ == "__main__":    import time    start = time.clock()    main()    print "%s sec" % (time.clock() - start)
时间: 2025-01-31 23:03:57

[LeetCode][Python]Integer to Roman的相关文章

LeetCode:Integer to Roman

1.题目名称 Integer to Roman (阿拉伯数字到罗马数字的转换) 2.题目地址 https://leetcode.com/problems/integer-to-roman 3.题目内容 英文:Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 中文:给出一个整数,将它转换成罗马数字.输入在1-3999之间. 4.题目分

LeetCode之Integer to Roman, Roman to Integer

罗马数字以前只接触过I到VIII,第一次听说罗马数字也可以表示大于8的数字.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则.Wiki了一把,又参考了其它的文档,总结如下: 罗马数字规则: 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 罗马数字中没有"0". 2, 重复次数:一个罗马数字最多重复3次. 3, 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字. 在较大的罗马数字的左边记上

LeetCode 012 Integer to Roman

[题目] Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. [题意] 给定一个整数,将其表示成罗马数字 [思路] 罗马数字中只使用如下七个基值字母:M,D,C,L,X,V,I,分别用来表示1000.500.100.50.10.5.1. 罗马数组数规则: 基本数字Ⅰ.X .C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成

【LeetCode】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. public class Solution { public String intToRoman(int num) { StringBuilder sb = new StringBuilder(); if(num==0) return sb.toString(); while(num

[LeetCode][JavaScript]Integer to Roman

Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. https://leetcode.com/problems/integer-to-roman/ 阿拉伯数字转罗马数字. 造表,把有所的基数都放到表中,主要是要放入IV,IX这类数,方便处理. 从大到小匹配表中的数,一轮循环搞定. 1 /** 2 *

leetCode 12. Integer to Roman | 字符串 | Medium

12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题目大意: 将一个给定的阿拉伯数字转换成罗马数字. 思路: 这题看到的时候,想的太多. 其实很简单,将千位,百位,十位,个位都表示出来,然后组合即可. 代码如下: class Solution { public:     string int

【LeetCode】Integer to Roman (2 solutions)

Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 逐区间做处理. 解法一:非递归 class Solution { public: string intToRoman(int num) { string ret; //M<-->1000 while(num >= 1000) { ret +=

[LeetCode] 012. Integer to Roman (Medium) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 012.Integer_to_Roman (Medium) 链接: 题目:https://oj.leetcode.com/problems/integer-to-roman/ 代码(github):https://github.com/illuz/leetcode 题意: 把十进制转为罗马数. 分析: 模拟即可.

LeetCode 12 Integer to Roman(C,C++,Java,Python)

Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. Solution: 根据数字将每一位转换为罗马字符串即可,时间复杂度O(len(num)) 题目大意: 给一个整数,将整数调整为罗马数字,关于罗马数字的定义见这里:罗马数字 个位数举例 Ⅰ,1 ]Ⅱ,2] Ⅲ,3] Ⅳ,4 ]Ⅴ,5 ]Ⅵ,6]Ⅶ,7] Ⅷ,8 ]Ⅸ