《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, ...
随机推荐
- ZOJ2208 To and Fro 2017-04-16 19:30 45人阅读 评论(0) 收藏
To and Fro Time Limit: 2 Seconds Memory Limit: 65536 KB Mo and Larry have devised a way of encr ...
- mysql 统计一个字符在字符串中出现的次数
CREATE FUNCTION `str_pcount`(str varchar(255),p varchar(255)) RETURNS int(11)BEGIN #统计一个字符在字符串中出 ...
- IPv4&&IPv6地址结构分析
IPv4套接字地址结构: 套接字都需要有一个指向套接字地址结构的指针作为参数.每个协议簇都定义它自己的套接字地址结构.这些结构的名字均已sockaddr_开头,并以对应每个协议族的唯一后缀结尾. wi ...
- tomcat启动时就频繁gc和full gc
一个小业务,流量并不大,功能也很简单,spring framework+mybatis+quartz,一启动就看到gc的频次和full gc的频次非常高: 4.202: [Full GC 4.202: ...
- osgi.net框架
osgi.net是一个动态的模块化框架.它向用户提供了模块化与插件化.面向服务构架和模块扩展支持等功能.该平台是OSGi联盟定义的服务平台规范移植到.NET的实现. 简介 尤埃开放服务平台是一个基于. ...
- 基于Quartz.net的远程任务管理系统-起绪
Quartz.net这一个任务调度框架,相信大部分的开发者都非常的熟悉了. 往往在一个项目之中,我们会有很多的定时任务,加之多人参与编码,难免会有些难于管理等问题.为统一编写规范,以及对定时任务的管理 ...
- Fiddler关闭后打不开网页
今天项目系统测试的时候,CS客户端的Restful请求都失败,但是实际上的服务是正常开启的,马上通过cmd指令ping了一下服务,正常:再用telnet试了一下端口,也是正常.不过随后发现在这台电脑上 ...
- sharepoint 2010 powershell
可参看云总的博客:http://blog.csdn.net/yun_liang1028/article/details/6419729
- 调用阿里云API 的demo示例(java/python)
Java 示例 // 创建DefaultAcsClient实例并初始化 DefaultProfile profile = DefaultProfile.getProfile(vo.getAliRegi ...
- BASE64编码乱码问题的浅层分析与解释
本文由作者朱臻授权网易云社区发布. 1问题案例 曾在开发过程中,我们遇到了BASE64编码乱码的问题,该问题的场景如下: 当web前端,将带有中文字符的字符串base64编码后,传到后端.当后端将数据 ...