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, ...
随机推荐
- [android 代码search地址]
http://www.androidcodesearch.com/source/packages/apps/Settings/src/com/android/settings/SecuritySett ...
- 长沙理工大学第十二届ACM大赛-重现赛
年轮广场 时间限制:1秒 空间限制:131072K 题目描述 在云塘校区,有一个很适合晒太阳的地方————年轮广场 年轮广场可以看成n个位置顺时针围成一个环. 这天,天气非常好,Mathon带着他的小 ...
- 卷积层feature map输出到文本
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/52166388 以VGG_16的网络为例 ...
- Mysql常见配置说明
[mysqld]配置 default-storage-engine = MyISAM 默认选择某种表存储引擎 ignore-bulidin-innodb 忽略mysql自带的innodb引擎,使用这个 ...
- *AtCoder Regular Contest 096F - Sweet Alchemy
$n \leq 50$的树,每个点有权值,现要选点(可多次选一个点)使点数尽量多,如下限制:选的总权值不超过$C \leq 1e9$:$c_i$表示$i$选的次数,$p_i$表示$i$的父亲,那么$c ...
- ci框架——数据库(增删改查)
1:配置数据库(application/config/database.php)修改 $db['default'] = array( 'dsn' => '', 'hostname' => ...
- sqlplus登陆scott用户,以及退出连接
进入sqlplus界面 即登陆成功,PLsql也一样 退出连接:
- iOS中创建自定义的圆角按钮
iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...
- 【转】C语言中整型运算取Ceiling问题
原文:http://blog.csdn.net/laciqs/article/details/6662472 --------------------------------------------- ...
- 赵雅智_Swift(2)_swift常量和变量
分号 Swift 并不强制要求你在每条语句的结尾处使用分号(;) 你打算在同一行内写多条独立的语句必需要用分号 let cat = "? ?? ? "; println(cat) ...