LeetCode题解(13)--Roman to Integer
https://leetcode.com/problems/roman-to-integer/
原题:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路:
关键是要搞清罗马数字规则。PS:不用考虑错误输入。
核心: 遍历一遍,相同字母合并成对应数,然后比较如果比它后面的小就减去,否则就加上。时间复杂度O(n)。
具体解析:
|
基本字符
|
I
|
V
|
X
|
L
|
C
|
D
|
M
|
|
相应的阿拉伯数字表示为
|
1
|
5
|
10
|
50
|
100
|
500
|
1000
|
class Solution {
public:
int romanToInt(string s) {
map<char,int> ro;
ro['I']=; ro['V']=; ro['X']=; ro['L']=; ro['C']=; ro['D']=; ro['M']=;
int n=s.size();
int i=,k=,tmp=;
while(i<n){
tmp=ro[s[i]];
while(i<n && s[i]==s[i+]){
tmp +=ro[s[i]];
i++;
}
if( i==n || ro[s[i]]>ro[s[i+]] )
k +=tmp;
else
k -=tmp;
i++;
}
return k;
}
};
LeetCode题解(13)--Roman to Integer的更多相关文章
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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, ...
随机推荐
- ASP.NET项目使用MYSQL数据库部署到IIS服务器找不到请求的.Net Framework Data Provider解决方案
使用MySQL开发过程中在自己的机器上跑项目是没有问题的,但在实际部署到服务器上的时候就发生“找不到请求的.Net Framework Data Provider解决方案”错误,在排除项目本身原因之后 ...
- iOS中的过滤器和正则表达式(NSPredicate,NSRegularExpression)
参考链接:http://www.cocoachina.com/industry/20140321/8024.html NSPredicate Cocoa提供了一个NSPredicate类,它用来指定过 ...
- iOS学习笔记13-网络(二)NSURLSession
在2013年WWDC上苹果揭开了NSURLSession的面纱,将它作为NSURLConnection的继任者.现在使用最广泛的第三方网络框架:AFNetworking.SDWebImage等等都使用 ...
- Maven常用参数及其说明【转:http://blog.csdn.net/wangjunjun2008/article/details/18982089】
Maven常用参数及其说明 -h,--help Display help information-am,--also-make ...
- 16.1113 模拟考试T3
城堡[问题描述]给定一张N个点M条边的无向连通图,每条边有边权.我们需要从M条边中选出N − 1条, 构成一棵树. 记原图中从 1 号点到每个节点的最短路径长度为?Di ,树中从 1 号点到每个节点的 ...
- noj 2068 爱魔法的露露 [线性扫一遍]
njczy2010 2068 Accepted 325MS 8052K 1450Byte G++ 2014-11-13 11:20:40.0 爱魔法的露露 时间限制(普通/Java) : 1200 ...
- python常用模块详解(一)
一.简介 模块是一个保存了Python代码的文件.模块能定义函数,类和变量.模块里也能包含可执行的代码 模块分为三种: 自定义模块 内置标准模块 开源模块(第三方) 自定义模块: 模块导入 impor ...
- hdu 4849
简单题,公式计算+最短路.注意点:注意1 取模,2 数组开到n*n+n. #include<iostream> #include<queue> using namespace ...
- ZOJ - 4016 Mergeable Stack (STL 双向链表)
[传送门]http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=4016 [题目大意]初始有n个空栈,现在有如下三种操作: (1) ...
- java集合系列之LinkedList源码分析
java集合系列之LinkedList源码分析 LinkedList数据结构简介 LinkedList底层是通过双端双向链表实现的,其基本数据结构如下,每一个节点类为Node对象,每个Node节点包含 ...