[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 ...
随机推荐
- android 连接网络的简单实例
1.android有两种连接网络的类HttpURLConnect和HttpClient,但是HttpClient已逐渐被HttpURLConnect类代替所以就不提及. 2.实例 String add ...
- VB2012读取xml
上回谢了生成写xml的,现在把读取的补上 文件如下 <?xml version="1.0" encoding="UTF-8" standalone=&qu ...
- ODI11G 在Linux上的安装配置
ODI11G 在Linux上的安装配置 OS环境:Red hat Linux x86_64 一.JDK安装 1. 去oracle官网上下载 http://www.oracle.com/technetw ...
- HibernateTransactionManager 和 hibernateTemplate的区别
在applicationContext.xml中有如下配置: <bean id="hibernateTemplate" class="org.springframe ...
- 汽车总线obd模拟器,obd仿真器,ecu模拟器,obd开发测试工具,可以模拟ecu中的obd协议 MRD-5050
汽车总线OBD模拟器MRD-5050型号是在车辆越来越趋于网络化的趋势下研发的,是汽车产品开发.调试.生产必备的工具,能为为开发人员节省大量的时间.当前车辆上的总线设备越来越多,有的高端车上甚至多到有 ...
- POC- Proof of Cocept -- 概念验证
POC,是Proof of Concept的缩写,意思是为观点提供证据,它是一套建议的电子模型,实例化代码,它可用于论证团队和客户的设计,允许评估和确认概念设计方案,POC的评价可能引起规格和设计的调 ...
- [问题解决] /home目录占用率100%
今天发现一个比较奇怪的现象,linux系统下一个目录挂在存储下,df -Th 显示该目录使用率100%, du 该目录显示只用了2%, 后来发现是由于进程占用了被删掉的文件空间导致.举例如下: [ro ...
- Delphi代码中嵌入ASM代码
前言 Delphi作为一个快速高效的开发平台,使用的人越来越多,但熟悉在Delphi代码中嵌入ASM代码的程序员我想不多,因为这方面的资料太少了,另一方面,它还需要有基本的汇编语言知识,关於汇编语言的 ...
- PLA能收敛的证明
题:如果资料D线性可分,PLA如何保证最后能得到最优解. 思路:假设$w_f$能够分割资料D,$w_{t+1}$经过更新$w_{t+1}=w_t + y_{n(t)}x_{n(t)}$后,与$w_f$ ...
- 【Stackoverflow好问题】Java += 操作符实质
问题 直到今天,我都一直以为: i += j 等同于 i = i + j; 但如果有: int i = 5; long j = 8; 这时 i = i + j不能编译.但i += j却能够编译.这说明 ...