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
 
规则:
1、相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
3、小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
4、正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
5、在一个数的上面画一条横线,表示这个数扩大1000倍。
 
有几条须注意掌握:
1、基本数字Ⅰ、X 、C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个;放在大数的左边只能用一个。
2、不能把基本数字V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目,只能使用一个。
3、V 和X 左边的小数字只能用Ⅰ。
4、L 和C 左边的小数字只能用X。
5、D 和M 左边的小数字只能用C。
 
我的AC代码:
 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的更多相关文章

  1. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  2. 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  ...

  3. 【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 ...

  4. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  5. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  6. 【leetcode】13. Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...

  7. 「Leetcode」13. Roman to Integer(Java)

    分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...

  8. 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 ,即 ...

  9. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

随机推荐

  1. HDU 3341 Lost's revenge

    Lost's revenge Time Limit: 5000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ...

  2. TOJ 5021: Exchange Puzzle

    5021: Exchange Puzzle  Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByteTotal Submit ...

  3. xmpp 登录注册小结

    将XMPPStream放在APPDelegate,以便全局访问 #pragma mark - XMPP相关的属性和方法定义 /** * 全局xmppstream,只读属性 */ @property ( ...

  4. 将RabbitMq用好需要了解的一些基础知识

    本文面向有一定RabbitMq基础的童鞋. 首先,我们来理理RabbitMq的一些基本概念: Connection: 客户端与RabbitMq服务器节点的Tcp链接. Channel: 信道,因为一条 ...

  5. POJ2560 Freckles

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description In an epis ...

  6. Codevs 2855 游乐园的迷宫

    2855 游乐园的迷宫  时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 迷宫可是每个游乐园必不可少的项目,菜菜当然是要尝试一下啦. ...

  7. 改变input的value值,同时在HTML中将value进行改变

    在使用lodop进行打印的时候,需求中有这样一个功能:某个字段可以在页面的input框中进行修改. 但是在进行打印时调用的是静态的HTML代码,这就导致在页面的input框中改变字段之后,但是HTML ...

  8. leetcode 331. Verify Preorder Serialization of a Binary Tree

    传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...

  9. 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 ...

  10. bigdata related

    hive: http://lxw1234.com/archives/2015/07/413.htm 搜狗实验室数据集: https://www.sogou.com/labs/resource/list ...