《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫《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]的更多相关文章
- LeetCode题解(13)--Roman to Integer
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...
- 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 ...
- 【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 ...
- 【一天一道LeetCode】#13. Roman to Integer
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【leetcode】13. Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...
- 「Leetcode」13. Roman to Integer(Java)
分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...
- 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 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
随机推荐
- Some_tools
Why and what There are lots of nice or great tools on the internet, sometimes I will just forget a p ...
- Android-Sqlite-SQL操作增删改查
一想到Android到数据库,只需要想到一个类 SQLiteOpenHelper,然后写一个类继承 SQLiteOpenHelper,重写构造方法,对数据库进行配置 public class MySQ ...
- TSQL--聚合函数
--======================================================== --COUNT --COUNT(1) 和COUNT(*) 计算结果相同,COUNT ...
- asp.net 类头部描述
这里教大家怎么在新建类的时候默认有头部描述,先看效果: 像这样的内容我们要怎么进行添加呢? 前方高能...... 找到VS的安装目录-->比如我自己的安装目录D:\VS2013\Common7\ ...
- [C#学习笔记]你真的理解拆箱装箱吗?
学习一项新知识的时候,最好的方法就是去实践它. 前言 <CLR via C#>这本神书真的是太有意思了!没错我的前言就是这个. 装箱 首先来看下,下面这段代码 可以看到,每次循环迭代都会初 ...
- trace sql log
C:\Windows\system32>cd /d d:\PSSDIAG\pssd_2k12_x64 d:\PSSDIAG\pssd_2k12_x64>pssdiag.cmd 2014/ ...
- Android App的破解技术有哪些?如何防止反编译?
现在最流行的App破解技术大多是基于一定相关技术的基础:如一定阅读Java代码的能力.有一些Android基础.会使用eclipse的一些Android调试的相关工具以及了解一些smali的语法规范 ...
- iOS 应用隐藏状态栏
有时候在 Storyboard 和 target 里的设定并不足以使得应用能够完全隐藏状态栏. 这时候需要到 Info.plist,添加一项:View controller-based status ...
- Django_Restframwork_序列号组件
第一种序列化方式. 第二种方法通过Model_to_dict方法进行创建 第三种方式序列号组件Serializers: 第四种方法序列化 五.ModelSerializer组件. POST校验 PU ...
- mysql遇到的问题:can't creat/write to file "/var/mysql/xxxx.MYI"
这个问题困扰了我,可能有两个原因. 1.文件夹权限不够,至少也要给出 USERS 组的可读可写权限: 2.文件夹的磁盘满了,文件写不进去了: 如果是这个不能创建和写的问题,很大的概率就是文件的权限.没 ...