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, ...
随机推荐
- HDU 3341 Lost's revenge
Lost's revenge Time Limit: 5000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ...
- TOJ 5021: Exchange Puzzle
5021: Exchange Puzzle Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit ...
- xmpp 登录注册小结
将XMPPStream放在APPDelegate,以便全局访问 #pragma mark - XMPP相关的属性和方法定义 /** * 全局xmppstream,只读属性 */ @property ( ...
- 将RabbitMq用好需要了解的一些基础知识
本文面向有一定RabbitMq基础的童鞋. 首先,我们来理理RabbitMq的一些基本概念: Connection: 客户端与RabbitMq服务器节点的Tcp链接. Channel: 信道,因为一条 ...
- POJ2560 Freckles
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description In an epis ...
- Codevs 2855 游乐园的迷宫
2855 游乐园的迷宫 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 迷宫可是每个游乐园必不可少的项目,菜菜当然是要尝试一下啦. ...
- 改变input的value值,同时在HTML中将value进行改变
在使用lodop进行打印的时候,需求中有这样一个功能:某个字段可以在页面的input框中进行修改. 但是在进行打印时调用的是静态的HTML代码,这就导致在页面的input框中改变字段之后,但是HTML ...
- leetcode 331. Verify Preorder Serialization of a Binary Tree
传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...
- Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]
传送门 E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- bigdata related
hive: http://lxw1234.com/archives/2015/07/413.htm 搜狗实验室数据集: https://www.sogou.com/labs/resource/list ...