[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 memcache
http://blog.csdn.net/newjueqi/article/details/8350643
- Java_SSH项目主要步骤记录
建立Spring-Struts-Hibernate的步骤整理 1. 建立web project 2. 建立hernate, action, service包 3. 右击项目,add myeclipse ...
- 10-3[RF] feature selection
main idea: 计算每一个feature的重要性,选取重要性前k的feature: 衡量一个feature重要的方式:如果一个feature重要,则在这个feature上加上noise,会对最后 ...
- paip.QQ音乐导出歌单总结
paip.QQ音乐导出歌单总结 作者Attilax , EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net/attilax ...
- Struts2 使用通配符动态请求Action
在以前的学习中,<action>元素的配置,都是用明确的配置,其name.class等属性都是一个明确的值.其实Struts2还支持class属性和method属性使用来自name属性的通 ...
- 【Struts2学习笔记(1)】Struts2中Action名称的搜索顺序和多个Action共享一个视图--全局result配置
一.Action名称的搜索顺序 1.获得请求路径的URI,比如url是:http://server/struts2/path1/path2/path3/test.action 2.首先寻找namesp ...
- ubuntu 下 apache+tomcat整合_(mod-jk方法)[转]
整合的平台是Centos5环境如下: apache源代码包: httpd-2.2.11.tar.gz 下载地址http://labs.xiaonei.com/apache-mirror/httpd/ ...
- Swift中编写单例的正确方式
在之前的帖子里聊过状态管理有多痛苦,有时这是不可避免的.一个状态管理的例子大家都很熟悉,那就是单例.使用Swift时,有许多方法实现单例,这是个麻烦事,因为我们不知道哪个最合适.这里我们来回顾一下单例 ...
- Mschart绘制图表之X轴为时间的设置方式
最近使用C#开发图表,比较了DirectorChart,DontNetCharting,TeeChart,最终选用微软的mschart开发,对于X轴作为时间轴探索了好久,终于实现了想要的效果. 界面效 ...
- oracle 如何重置用户密码