[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 ...
随机推荐
- C# 获得当前应用程序路径
1.获得当前应用程序的路径最稳定的方法:AppDomain.CurrentDomain.BaseDirectory 生成的路径:../项目名称/bin/Debug下的路径
- 利用Apperance协议定义View的全局外观
假设要定义一个全局的bkColor用于背景颜色 1.@property(nonatomic,strong)UIColor *bkColor UI_APPEARANCE_SELECTOR; 2.在下面方 ...
- 浅谈JSP(二)
一.EL表达式 作用:从作用域(pageContext,request,session,application)中取值,并显示在页面中. 本质:用于替换输出脚本(<%= %>). 1.从作 ...
- 如何在Mac系统安装eclipse并运行java程序?
链接地址:http://jingyan.baidu.com/article/7f41ecece8ef5b593c095c71.html eclipse现在也有 Mac版了,我们快来试一试吧!现在我将带 ...
- Stack(栈)
Stack(栈)是一种后进先出的数据结构,下面介绍一下栈的具体运用: 一.Stack 中的 empty 函数 stack<int> s( 5 , 10) ; s.empty() ; ...
- mysql 锁表查询及其处理
1.show OPEN TABLES where In_use > 0; 2.show processlist; 3.kill thread_id; 其中 thread_id为processli ...
- A Byte of Python 笔记(9) 面向对象编程
第11章 面向对象编程 面向过程:根据操作数据的函数或语句块来设计程序. 面向对象(OOP, object-oriented programming):把数据和功能结合起来,用对象包裹组织程序. 类 ...
- IOS 页面之间的传值(主讲delegate)
IOS的Delegate,通俗一点说就是页面之间的传值. 总结一下现在知道的IOS页面之间传值的方式有三种 1.使用NSNotification发送通知的传值 主要是通过NSNotificationC ...
- night Mode 夜间模式css
*,*:before,*:after,html[mode='nightmode'] * { color: #61615f !important; border-color: #212a32 !impo ...
- 【转】vs2008中leptonica-1.68安装配置
tesseract ocr挺不好配置的,找到一篇不错的文章,分享如下:http://hi.baidu.com/ever8936/blog/item/6998e1196b1d0161dab4bd8f.h ...