[LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/roman-to-integer/
Roman to Integer Given a roman numeral, convert it to an integer. 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之类的表示。 可以从右往左,依次处理:
当遇到这个字母表示的数字比后一个小的时候,减去这个数;否则,累加。
''' class Solution:
# @return an integer
def romanToInt(self, s):
roman_dict = {"I":1, "V":5, "X":10, "L":50, "C":100, "D":500, "M":1000}
if len(s) == 0:
return 0
value = 0
for i in xrange(len(s)-1, -1, -1):
if i < len(s)-1 and roman_dict[s[i]] < roman_dict[s[i+1]]:
value = value - roman_dict[s[i]]
else:
value = value + roman_dict[s[i]]
return value def main():
s = Solution()
print s.romanToInt("MCMXCIX") if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)
[LeetCode][Python]Roman to Integer的更多相关文章
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [LeetCode] 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】Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- 【JAVA、C++】LeetCode 013 Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- rabbitmq问题之HTTP access denied: user 'guest' - User can only log in via localhost
问题: 昨天安装rabbitmq(3.3.4版本)服务,并启用rabbitmq_management插件去管理rabbitmq服务,但是在访问管理界面使用guest用户登录时出现login faile ...
- Static Function Test
public class StaticTestCls { public int x = 0; public static int y = 0; private void SetValue_Object ...
- Android系统Root原理初探(转)
http://www.imooc.com/learn/126 chkconfig setup 解压update.zip这个文件,可发现它一般打包了如下这几个文件: 或者没有updates而是syste ...
- 通过jpegoptim批量压缩文件
#!/bin/sh filelist=$(ls) for file in $filelist do if [ -d $file ] then du -h $file /usr/local/bin/jp ...
- GDI编程
图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏幕输出.GDI负责Wind ...
- 关于IE的兼容模式
前言 为了帮助确保你的网页在所有未来的IE版本都有一致的外观,IE8引入了文件兼容性.在IE6中引入一个增设的兼容性模式,文件兼容性使你能够在IE呈现你的网页时选择特定编译模式. 新的IE为了确保网页 ...
- 如何对n个数进行排序,要求时间复杂度O(n),空间复杂度O(1)
看上去似乎任何已知的算法都无法做到,如果谁做到了,那么所有的排序方法:QuickSort,ShellSort,HeapSort,BubbleSort等等等等,都可以扔掉了,还要这些算法干吗阿,呵呵.不 ...
- BZOJ 3237([Ahoi2013]连通图-cdq图重构-连通性缩点)
3237: [Ahoi2013]连通图 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 106 Solved: 31 [ Submit][ St ...
- andengine游戏引擎总结进阶篇2
本篇包括瓦片地图,物理系统, 1瓦片地图 超级玛丽,冒险岛,魂斗罗等游戏主场景都有瓦片地图画成,它的作用可见一斑,它可以用tiled Qt软件画成,在辅助篇中讲讲解tiled Qt软件的使用 1)加载 ...
- [置顶] 浅析objc的消息机制
学习ios的同学都知道ojbc一种runtime的语言,runtime表明函数的真正执行的时候来确定函数执行的.这样的好处就是我们能很灵活的设计我们的代码,也能在看似合法的情况下做一些非常有意思的事情 ...