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","D","DC","DCC","DCCC","CM"]
    	X=["","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"]
    	I=["","I","II","III","IV","V","VI","VII","VIII","IX"]

    	return M[num/1000]+C[(num%1000)/100]+X[(num%100)/10]+I[(num%10)]

  

时间: 2024-07-31 11:50:09

Leetcode 12. Integer to Roman(python)的相关文章

LeetCode 12 Integer to Roman(整型数到罗马数)

翻译 给定一个整型数值,将其转换到罗马数字. 输入被保证在1到3999之间. 原文 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 我不会告诉你一开始我是用的无数个变量和if-- 后来实在受不了这么多变量就将其写成了枚举,那么接下来就迎刃而解了. 为了让大家理解罗马数是怎么计数的,这里我截了一张图,具体的大家可以自行用微软Bing

LeetCode 12 Integer to Roman (整数转罗马数字)

题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", "M", "MM", "MMM”};//1000~3000String C[] = {"", "C", "CC", "CCC", "CD", "D&quo

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

012 Integer to Roman(Java)

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 先是了解罗马数的构成 基本字符 I V X L C D M 相应的阿拉伯数字表示为 1 5 10 50 100 500 1000 相同的数字连写.所表示的数等于这些数字相加得到的数.如:Ⅲ=3: 小的数字在大的数字的右边.所表示的数等于这些数字相加得到的数. 如:Ⅷ=8.Ⅻ=12:

[LeetCode][12]Integer to Roman解析 int转罗马字符时间复杂度为常数的实现-Java实现

Q: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. A: 俺是真不知道啥是roman,我去查了一下合着是罗马字体,这题目意思也很简单就是说我要输入一个数字可以保证是1-3999但是你要把这个阿拉伯数字转换成罗马数字. 首先我们要清楚罗马数字怎么表示的,罗马数字表示如下: 罗 马 数字共有七个,即I(1),V(5),X(10),L

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

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,5,10.  1到3之间用重复的1表示,4用IV 90用XC 依次类推. I = 1; V = 5; X = 10; L = 50; C = 1