[LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
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 - 1
V - 5
X - 10
L - 50
C - 100
D - 500
M - 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)
[LeetCode][Python]Integer to Roman的更多相关文章
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- Leetcode 12——Integer to Roman
12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ...
- Leetcode 12. Integer to Roman(打表,水)
12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ...
- [LeetCode&Python] Problem 13. Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 【leetcode❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 【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 ...
随机推荐
- MySql 优化 网上资料
1.选取最适用的字段属性 MySQL可以很好的支持大数据量的存取,但是一般说来,数据库中的表越小,在它上面执行的查询也就会越快.因此,在创建表的时候,为了获得更好的性能,我们可以将表中字段的宽度设得尽 ...
- android 快速创建一个新的线程
要给一个activity做成子线程的模式 第一种:直接创建子线程并启动 private Thread newThread; //声明一个子线程 new Thread() { @Override pub ...
- ecshop给虚拟商品添加出售和未出售的导出xlc
在admin/virtral_card.php文件中找到$_REQUEST['act'] == 'card'这里是用来显示某一个虚拟商品的出售记录的列表将会发送到replenish_list.htm在 ...
- Linq to sql 操作
1.往数据库添加数据 NorthwindDataContext abc = new NorthwindDataContext(); abc.Log = Console.Out; User a = ne ...
- javascript - 工作笔记 (事件三)
有关定义就不多说了,事件分两种 一,冒泡型事件 这是IE浏览器中事件模型的实现,顾名思义,就像水中的泡一样,自底而上,其经过的父元素都会触发对应的事件. 即:触发元素的父元素先于触发元素触发,看dem ...
- android中利用view画出一条竖线
在android中有时候需要通过线条来分割控件.最常见的情形就是在底部选项卡的多个button中间,通过加入一条竖线加以区分或者是在头部导航添加 竖线,将返回键和其他内容区分开来.一般会通过image ...
- umdh工具使用
先安装工具,http://msdn.microsoft.com/en-us/windows/hardware/gg463009.aspx 选择其中的http://msdn.microsoft.com/ ...
- AndroidUI 视图动画-移动动画效果 (TranslateAnimation)
移动动画效果可以使用 TranslateAnimation; <Button android:id="@+id/btnTranslate1" android:layout_w ...
- 使用Transaction访问数据库(C#,TransactionScope,.NET 2.0)
针对SQL2005和.NET 2.0的事物机制有了新的突破传统数据库事物访问机制,代码如下: 或者这种,其实都差不多 ...
- 关于c++primer的一个代码错误
近期看c++primer第四版的标准容器vector.讲到对vector容器的插入删除元素会使得end()的迭代器失效的问题,所以不建议程序猿对end()的存储. vector<int> ...