我现在在做一个叫《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

思路

首先要知道罗马数字的规律:

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1,000

然后还有一个规则是,罗马数字从左自右相加,但是如果小数字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

《LeetBook》leetcode题解(13):Roman to Integer[E]的更多相关文章

  1. LeetCode题解(13)--Roman to Integer

    https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...

  2. C# 写 LeetCode easy #13 Roman to Integer

    13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and  ...

  3. 【LeetCode】13. Roman to Integer (2 solutions)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  4. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  5. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  6. 【leetcode】13. Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...

  7. 「Leetcode」13. Roman to Integer(Java)

    分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...

  8. 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 ,即 ...

  9. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

随机推荐

  1. 团体程序设计天梯赛L2-024 部落 2017-04-18 11:31 274人阅读 评论(0) 收藏

    L2-024. 部落 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不 ...

  2. Ubuntu再图形登录中以root的身份进入???

    Ubuntu再图形登录中以root的身份进入??? 这样做的需求,应该就是,可以再图形页面以root的身份进行图形化操作,比较方便更改配置文件. 1. 可以实现,但是不建议这么做,之后会出现一个警告提 ...

  3. Computer

    Computer 1. Ctrl+N .根据惯例,“Control”.“Shift” 以及 “Alternate” 按键将以 Ctrl.Shift 以及 Alt 来表示,需要特别指出的是,其中第一个按 ...

  4. 2.自己的Github注册流程

    一开始申请Github,说实话我真的不知道它是什么东西,而且有什么用途.然后我就用360百科搜索了一下有关它的介绍:. 而说明的是Git是一个分布式的版本控制系统.然后我进入官方网站进行账号注册,而注 ...

  5. EAS_AOP分布式事务

    在System.Transactions事务体系中,为事务提供了7种不同的隔离级别.这7中隔离级别分别通过 System.Transactions.IsolationLevel的7个枚举项表示. pu ...

  6. c# 检查报错详细

    catch (DbEntityValidationException error) { string test = string.Empty; foreach (var validationError ...

  7. Mysql修改字段类型

    mysql 修改字段长度 alter table news  modify column title varchar(130); alter table 表名 modify column 字段名 类型 ...

  8. Android intent 传值不更新的原因和解决办法

    当 Activity 的启动模式是 singleTask 或者 singleInstance 的时候.如果使用了 intent 传值,则可能出现 intent 的值无法更新的问题.也就是说每次 int ...

  9. RabbitMq初探——消息均发

    消息均发 前言 由前文 RabbitMq初探——消息分发 可知,rabbitmq自带分发机制——消息会按顺序的投放到该队列下的多个消费者,例如1,3,5投放消费者C1,2,4,6投放消费者C2. 这就 ...

  10. jQuery中的each, data, 插件

    一.  each() $(' ').each(function (){...}) jQuery.each(collection, callback(indexInArray, valueOfEleme ...