[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 ...
随机推荐
- NetworkManager配置网络——Red Hat 7 && CGSL V5
NetworkManager服务管理网络方便在哪? 很重要的一点是:一个设备可以对应多个配置文件,但是同一时间只能有一个配置文件生效,这对于频率切换网络环境是非常方便的,不用再跑那个目录下去改配置 ...
- 设置高级的Logstash 管道
设置高级的Logstash 管道: 一个Logstash 管道在很多实用例子有一个或者多个输入,filter,和output 插件. 本节中 创建Logstash 配置文件来指定那些插件和讨论每个插件 ...
- Android 数字签名学习笔记
Android 数字签名学习笔记 在Android系统中,所有安装到系统的应用程序都必有一个数字证书,此数字证书用于标识应用程序的作者和在应用程序之间建立信任关系,如果一个permission的pro ...
- Struts2 一、 视图转发跳转
<struts> <constant name="struts.118n.encoding" value="UTF-8"></co ...
- Linux学习十八之、善用判断式
原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_3.php 善用判断式 在第十一章中,我们提到过 $? 这个变量所 ...
- hdu 3068 最长回文(manachar求最长回文子串)
题目连接:hdu 3068 最长回文 解题思路:通过manachar算法求最长回文子串,如果用遍历的话绝对超时. #include <stdio.h> #include <strin ...
- 传iWatch 将在7月投入生产,10月出货,支持无线充电、触控、測量脉搏
今天又有关于 iWatch 的传言传出.据路透社的线人消息称,台湾的广达电脑(Quanta Computer Inc.)将于 7 月開始生产 iWatch.10 月出货,估计推出后首年的出货量为 50 ...
- iSCSI存储系统知识
一.概述 SCSI 即小型计算机系统接口(Small Computer System Interface:简写:SCSI),一种用于计算机和外部设备之间(硬盘.光驱.软驱.打印机等)系统级接口的独立处 ...
- 编程好帮手----CodeSmith Generator Studio
这是一个很好用的代码生成器,可以将数据库中的表生成类,这是和表中的字段一一对应这就很给力了,方便准确
- 《Microsoft SQL Server企业级平台管理实践》笔记
- 页是 SQL Server 中数据存储的基本单位,大小为 8KB. - 区是空间管理的基本单位,8个物理上连续的页的集合(64KB). - 页的类型包括: 1. Data 2. Index 3. ...