12. Integer to Roman Leetcode Python

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

建两个list依次match 罗马数字。 比较特殊的是 一些有两位的罗马数字。

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

这种做法是看到最好懂的,所以就用了。

class Solution:
    # @return a string
    def intToRoman(self, num):
        values=[1000,900,500,400,100,90,50,40,10,9,5,4,1]
        roman=['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
        list=''
        for i in range(len(values)):
            while num>=values[i]:
                num-=values[i]
                list+=roman[i]
        return list
        
时间: 2024-08-27 23:57:55

12. Integer to Roman Leetcode Python的相关文章

Leetcode 12. Integer to Roman(python)

class Solution(object): def intToRoman(self, num): """ :type num: int :rtype: str """ M=["","M","MM","MMM"] C=["","C","CC","CCC","CD",&

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

Integer to Roman leetcode java

题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解: 这道题..还有哪个roman to integer..第一件事 就是先把roman认全吧.. 罗马数字拼写规则(转自Wikipedia:http://zh.wikipedia.org/wiki/%E7%BD%97%E9%A9%AC%E6%95%B0%E5%AD%97)

12. Integer to Roman && 13. Roman to Integer && 273. Integer to English Words

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. Hide Tags Math String Hide Similar Problems (E) Roman to Integer (H) Integer to English Words public class Solution { pub

leedCode练题——12. Integer to Roman

1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, two is written as II in Roman numeral, just two one's added together. Twelve i

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 ]Ⅸ

[LeetCode] 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. 解释: 罗马数字采用七个罗马字母作数字.即Ⅰ(1).X(10).C(100).M(1000).V(5).L(50).D(500).记数的方法: 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3: 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8.

【leetcode】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. 解题思路: 注意到用例比较少,所以采用以空间换时间的方法,把所有的结果列出,然后组合出输入值n的字符串即可. 具体代码: 1 public class Solution { 2 public static String intToRoman(int num) { 3 St

LeetCode 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 与罗马字符转数字类似,注意按级处理. string intToRoman(int num) { string sRet; int iNum = num; while(iNum > 0) { if(iNum < 5) { if(iNum == 4) { sRet += "IV