《LeetBook》leetcode题解(13):Roman to Integer[E]
来源:互联网 发布:行知职高厨师学费 编辑:程序博客网 时间:2024/05/22 17:42
我现在在做一个叫《leetbook》的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看
书的地址:https://hk029.gitbooks.io/leetbook/
013. Roman to Integer
问题
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this question
思路
首先要知道罗马数字的规律:
然后还有一个规则是,罗马数字从左自右相加,但是如果小数字A在大数字B之前,表示B-A
VI = 5+1 = 6
IV = 5-1 = 4
因此,利用2个变量保存当前数字和之前的数字就行了。因为这题很简单,用python比较方便,我就用python做的
class Solution(object): def romanToInt(self, s): sum=0 pre = 2000 cur = 0 Map = {'I':1,'V':5,'X':10,'L':50,'C':100,'D':500,'M':1000} for i in range(len(s)): cur = Map[s[i]] sum = sum+Map[s[i]] if cur > pre : sum = sum-2*pre pre = cur return sum
0 0
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
- 《LeetBook》leetcode题解(8): String to Integer (atoi) [E]——正负号处理
- LeetCode题解-13-Roman to Integer
- LeetCode题解:Roman to Integer
- LeetCode题解:Integer to Roman
- LeetCode题解:Roman to Integer
- LeetCode题解:Integer to Roman
- LeetCode 题解(58): Integer to Roman
- LeetCode 题解(73): Roman to Integer
- leetcode题解||Integer to Roman问题
- leetcode题解 || Roman to Integer问题
- LeetCode题解——Roman to Integer
- LeetCode题解——Integer to Roman
- LeetCode 12 Integer to Roman题解
- LeetCode题解-12-Integer to Roman
- 12. Integer to Roman LeetCode题解
- 13. Roman to Integer LeetCode题解
- 分享资源
- Spring:java.lang.NoSuchMethodException: <init>()的解决办法
- nested exception is java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config
- Markdown常用基本语法汇总
- 自定义组件系列---第一篇
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
- 深入理解RunLoop
- Android Binder机制(3) 本地服务注册过程
- 字幕整理:Fei-Fei Li: How computer understand the world?
- 网络:对称加密
- 初步认识Highcharts
- android-pulltorefresh设置header的问题
- codeforces 618F
- 用户登陆设计