[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):
class Solution(object):
def intToRoman(self, num):
"""
:type num: int
:rtype: str
"""
a = [1000,900,500,400,100,90,50,40,10,9,5,4,1]
b = ['M','CM','D','CD','C','XC','L','XL','X','IX','V','IV','I']
ans = ''
i = 0
count = 0
while num > 0:
count = num/a[i]
num %= a[i]
while count > 0:
ans += b[i]
count -= 1
i += 1
return ans
转载请注明出处:http://www.cnblogs.com/chruny/p/4817819.html
[LeetCode]题解(python):012-Integer to Roman的更多相关文章
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- No.012 Integer to Roman
12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- LeetCode--No.012 Integer to Roman
12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...
- 【LeetCode】012. Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【JAVA、C++】LeetCode 012 Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [Leetcode]012. Integer to Roman
public class Solution { public String intToRoman(int num) { String M[] = {"", "M" ...
- leetcode解决问题的方法||Integer to Roman问题
problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...
- leetcode第12题--Integer to Roman
Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...
- 012 Integer to Roman 整数转换成罗马数字
给定一个整数,将其转为罗马数字.输入保证在 1 到 3999 之间. 详见:https://leetcode.com/problems/integer-to-roman/description/ cl ...
随机推荐
- Android快速开发框架——AndroidAnnotations(Code Diet)
简介:AndroidAnnotations是一个依赖注入方式来简化代码结构 ,快速开发的开源框架,使结构代码更清晰,减少代码重复性.对今后我们做自动化测试和自动化埋点开发都会提高开发效率.跟我们之前使 ...
- android 背景透明度渐变动画
button.setVisibility(View.VISIBLE); // 背景透明度渐变动画 ObjectAnimator alpha = ObjectAnimator.ofFloat(butto ...
- HDU 5875 Function(ST表+二分)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5875 [题目大意] 给出一个数列,同时给出多个询问,每个询问给出一个区间,要求算出区间从左边开始不 ...
- swig模板 html代码自然状态下输出是转义的,必须加一个函数来转换为html代码;
<div>{{o.content|raw}}</div> |raw 相当于一个函数,转义函数,最终输出结果为html代码:
- HDU ACM 1063 Exponentiation 大实数乘方
分析:大实数乘方计算. #include<iostream> #include<string> using namespace std; struct BigReal //高精 ...
- 再学习sqlhelper
在机房收费重构系统的时候,第一次学习sqlhelper.当时感觉比较简单,没有写博客总结,现在又经过了图书馆的学习,感觉还是有必要写一写的. SqlHelper是一个基于.NETFramework的数 ...
- [转] 使用SQL脚本查看表空间使用率和使用dba_tablespace_usage_metrics视图的差别
传统的SQL脚本查看表空间使用率,使用的关键视DBA_DATA_FILE和DBA_FREE_SPACE. Oracle 11g引入了DBA_TABLESPACE_USAGE_METRICS视图.其实, ...
- 新浪微博 2.4sdk 一闪而过
解决方法,保持 ios应用中的 build id和 开放平台中填写的一致
- jar 包和批量处理
打jar包: 选择要打包的文件: javac -d . Hello.java .代表打包的目标路径 jar cvf test.jar com //test是打包名称,com是打包的文件夹 修改tes ...
- BZOJ 1485: [HNOI2009]有趣的数列( catalan数 )
打个表找一下规律可以发现...就是卡特兰数...卡特兰数可以用组合数计算.对于这道题,ans(n) = C(n, 2n) / (n+1) , 分解质因数去算就可以了... -------------- ...