[LeetCode]题解(python):013-Roman to Integer
题目来源:
https://leetcode.com/problems/roman-to-integer/
题意分析:
这道题目和上一题目相反,是将罗马数字转化成阿拉伯数字。
题目思路:
只要知道罗马数字和阿拉伯数字之间是怎么转换的就可以了。先做一个字符和数值对应的字典,{'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000};如果发现输入的字符串后一位比前一位小,则是出现4,9之类的,那么将前一个字符对应的数值减去两次就可以了。
代码(python):
class Solution(object):
def romanToInt(self, s):
"""
:type s: str
:rtype: int
"""
d = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000}
ans = 0
size = len(s)
i = 0
while i < size:
if i > 0 and d[s[i]] > d[s[i - 1]]:
ans += d[s[i]] - 2 * d[s[i - 1]]
else:
ans += d[s[i]]
i += 1
return ans
转载请注明出处:http://www.cnblogs.com/chruny/p/4817835.html
[LeetCode]题解(python):013-Roman to Integer的更多相关文章
- [LeetCode 题解]: Interger to Roman
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given an i ...
- 【LeetCode算法-13】Roman to Integer
LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- LeetCode--No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- 【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 ...
- 【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 ...
- [Leetcode]013. Roman to Integer
public class Solution { public int romanToInt(String s) { if(s == null || s.length() == 0) return 0; ...
- leetcode第13题--Roman to Integer
Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
随机推荐
- 脑波设备mindwaveTGC接口示例
TGC是一个后台应用程序,它负责和脑波设备建立连接,并获取数据,另一方面,它打开了一个端口在监听,让二次开发的应用程序,可以通过socket连接到这个TGC后台程序,获取脑波数据并展示,这种接口适合非 ...
- Android模拟器PANIC: Could not open:问题解决方法
最主要的就是环境变量没有配置或者我们使用的是绝对路径配置环境变量.这时我们只需要修改一下Android的环境变量就可以了. 具体解决方法如下: ①在环境变量中创建变量名:ANDROID_SDK_HOM ...
- swith 语句详解
switch 语句的格式: switch ( 整型或字符型变量 ) { case 变量可能值1 : 分支一; break; case 变量可能值2 : 分支二; break; case 变量可 ...
- css6种隐藏元素的方法
6种方式可以隐藏一个元素: 1 CSS display的值是none.(该元素是不会在页面上显示) 2 type="hidden"的表单元素.(该元素是不会在页面上显示) ...
- MFC 简单实现 DES 算法
前言 徐旭东老师说过学者就应该对知识抱有敬畏之心,所以我的博客的标题总喜欢加上"简单"二字,就是为了提醒自己,自己所学知识只是皮毛,离真理还远矣. DES 算法 DES算法是密码体 ...
- 前端笔试题 JS部分
题目 http://www.itmian4.com/forum.php?mod=viewthread&tid=4540 http://www.itmian4.com/forum.php?mod ...
- SPSS19.0实战之聚类分析
这篇文章与上一篇的回归分析是一次实习作业整理出来的.所以参考文献一并放在该文最后.CNBlOG网页排版太困难了,又不喜欢live writer…… 聚类分析是将物理或者抽象对象的集合分成相似的对象类的 ...
- 捕捉小括号获取的内容保存在RegExp的$1 $2..属性中
~~~~捕捉小括号获取的内容保存在RegExp的$1 $2..属性中 var reg=/^(-?\d+)(px|pt|em|in)?$/;if(reg.test(svalue)){ ...
- 关于LD_DEBUG (转载)
引用 LD_DEBUGThe dynamic library loader used in linux (part of glibc) has some neat tricks. One of the ...
- golang ODBC 访问access数据库(问题解决之心理路程)
最近项目需要,需要操作access,以前是用VC++ OLE访问,网络用ACE库,感觉很庞大...决定用go试试 网上用的最多的就是这个https://github.com/weigj/go-odbc ...