[LeetCode]题解(python):012-Integer to Roman

题目来源:

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



题意分析:

这道题是要把在区间[1-3999]的数字转化成罗马数字。



题目思路:

只要知道了罗马数字和阿拉伯数字是怎么转换的就不难了,要注意的是900,500,400,90,50,40,9,5,4分别应该是‘CM’,‘D’,‘CD’,‘XC’,‘L’,‘XL’,‘IX’,‘V’,‘IV’。



代码(python):

 1 class Solution(object):
 2     def intToRoman(self, num):
 3         """
 4         :type num: int
 5         :rtype: str
 6         """
 7         a = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
 8         b = [‘M‘,‘CM‘,‘D‘,‘CD‘,‘C‘,‘XC‘,‘L‘,‘XL‘,‘X‘,‘IX‘,‘V‘,‘IV‘,‘I‘]
 9         ans = ‘‘
10         i = 0
11         count = 0
12         while num > 0:
13             count = num/a[i]
14             num %= a[i]
15             while count > 0:
16                 ans += b[i]
17                 count -= 1
18             i += 1
19         return ans



转载请注明出处:http://www.cnblogs.com/chruny/p/4817819.html

时间: 2024-07-29 16:02:34

[LeetCode]题解(python):012-Integer to Roman的相关文章

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] 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】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. 题解: 观察 1 到 10 :Ⅰ,Ⅱ,Ⅲ,Ⅳ(IIII),Ⅴ,Ⅵ,Ⅶ,Ⅷ,Ⅸ,Ⅹ,Ⅺ 难点在于出现字符不能连续超过三次,以及大数左边至多有一个小数.所以要分情况:1 - 3,4,5 - 8,9.将小数在大数左边的情况摘出来. Solution 1 贪心策略 1 class Solut

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~3999之间的整型数字转换为罗马数字并输出. 解这道题我们必须了解罗马字母与整数之间的对应: 对照举例如下: AC代码 class Solution { public: string intToRoman(int num) { //存储罗马数字 st

leetcode第12题--Integer to Roman

Problem: 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 然后写一个子函数,输入数字和相应的位数级别,如个位为level 1,千为4.因为最多不会超过四千.所以可以如下.注意了,string用法很好

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解决问题的方法||Integer to Roman问题

problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 将1-3999的整数转换为罗马数字 thinking: (1) 对比举例 个位数举例 Ⅰ,1 ]Ⅱ.2] Ⅲ,3] Ⅳ,4 ]Ⅴ.5 ]Ⅵ,6]Ⅶ.7] Ⅷ,8 ]Ⅸ.9 ] 十位数举例 Ⅹ.10] Ⅺ,11 ]Ⅻ,12] XIII,13] XIV,14] XV,1

LeetCode题解 #7 Reverse Integer

上一次做完一个中等题觉得还挺简单,做一下简单题看看. 题目大意:给定一个整型(即int),将它的数位反过来,如321变为123,-321变为-123. 做了很久,因为简单题耗时的地方在于恶心!! 最后才看见溢出的时候应该返回0!! 一点都不难,特别是对于java. 我的思路: 1.将读入的int型包装成Integer,然后在转换成String.(看看前面有没有+-号) 2.将String 倒置,再转换为long.(一定要是long,否则无法判断溢出) 3.如果没溢出,再将long转换为int.

[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 Daba

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med