419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer.
The answer is guaranteed to be within the range from 1 to 3999.
IV -> 4
XII -> 12
XXI -> 21
XCIX -> 99
题意
计数方法:
| 基本字符 |
I
|
V
|
X
|
L
|
C
|
D
|
M
|
|---|---|---|---|---|---|---|---|
| 相应的阿拉伯数字表示为 |
1
|
5
|
10
|
50
|
100
|
500
|
1000
|
- 相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=3;
- 小的数字在大的数字的右边、所表示的数等于这些数字相加得到的数、 如:Ⅷ=8、Ⅻ=12;
- 小的数字(限于 I、X 和 C)在大的数字的左边、所表示的数等于大数减小数得到的数、如:Ⅳ=4、Ⅸ=9;
- 正常使用时、连写的数字重复不得超过三次;
- 在一个数的上面画一条横线、表示这个数扩大 1000 倍。
组数规则:
- 基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个;
- 不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个;
解法一:
class Solution {
public:
/*
* @param s: Roman representation
* @return: an integer
*/
int romanToInt(string s) {
int ans = ;
ans = toInt(s[]);
for (int i = ; i < s.length(); i++) {
ans += toInt(s[i]);
if (toInt(s[i-]) < toInt(s[i])) {
ans -= toInt(s[i-]) * ;
}
}
return ans;
}
int toInt(char s) {
switch(s) {
case 'I':return ;
case 'V':return ;
case 'X':return ;
case 'L':return ;
case 'C':return ;
case 'D':return ;
case 'M':return ;
}
return ;
}
};
419. Roman to Integer【medium】的更多相关文章
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 61. Search for a Range【medium】
61. Search for a Range[medium] Given a sorted array of n integers, find the starting and ending posi ...
- 62. Search in Rotated Sorted Array【medium】
62. Search in Rotated Sorted Array[medium] Suppose a sorted array is rotated at some pivot unknown t ...
- 74. First Bad Version 【medium】
74. First Bad Version [medium] The code base version is an integer start from 1 to n. One day, someo ...
- 75. Find Peak Element 【medium】
75. Find Peak Element [medium] There is an integer array which has the following features: The numbe ...
- 2. Add Two Numbers【medium】
2. Add Two Numbers[medium] You are given two non-empty linked lists representing two non-negative in ...
- 92. Reverse Linked List II【Medium】
92. Reverse Linked List II[Medium] Reverse a linked list from position m to n. Do it in-place and in ...
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 159. Find Minimum in Rotated Sorted Array 【medium】
159. Find Minimum in Rotated Sorted Array [medium] Suppose a sorted array is rotated at some pivot u ...
随机推荐
- FFmpeg YUV2RGB
AVFrame* YUV2RGB( AVCodecContext * avctx, AVFrame * frame ) { AVFrame* pFrameRGB=NULL; pFrameRGB=avc ...
- mysql下float类型使用一些误差详解
单精度浮点数用4字节(32bit)表示浮点数采用IEEE754标准的计算机浮点数,在内部是用二进制表示的如:7.22用32位二进制是表示不下的.所以就不精确了. mysql中float数据类型的问题总 ...
- web.xml加载顺序 [转载]
一 1.启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点. 2.紧急着,容创建一个Ser ...
- Node.js:EventEmitter类
一.EventEmitter 类 Node.js 所有的异步 I/O 操作在完成时都会发送一个事件到事件队列. Node.js里面的许多对象都会分发事件:一个net.Server对象会在每次有新连接时 ...
- hybrid app开发中用到的html5新特性localStorage、sessionStorage和websql database
近期在项目中进行hybrid app开发,项目中有大量的js代码执行在android设备上. 使用到了非常多HTML5的新特性,之前没有遇到过,不了解.这里记录下添加点前端的知识.混合式app开发中. ...
- (转)AS3 中,Function.apply、call中第一个参数的作用;与什么时候用
http://blog.csdn.net/linjf520/article/details/8746064 大家在使用Function.apply或是call时,是否发现,第一个参数不知道怎么用,赋值 ...
- Andrew Ng机器学习笔记+Weka相关算法实现(四)SVM和原始对偶问题
这篇博客主要解说了Ng的课第六.七个视频,涉及到的内容包含,函数间隔和几何间隔.最优间隔分类器 ( Optimal Margin Classifier).原始/对偶问题 ( Primal/Dual P ...
- mysql查找数据库中是否已经存在某张表
Sql: select count(*) from information_schema.TABLES t where t.TABLE_SCHEMA ="数据库名" and t.T ...
- CSS如何实现自定义鼠标应用到整个网页
在CSS区把body改为*即可.但是注意Firefox不支持ani和cur文件,如果您一定想使用ani和cur的指针,可以试着将它改名为.gif文件.实际测试如果改为gif则IE也不支持了.
- eclipse debug 错误 之 processWorkerExit
eclipe 在debug模式下,有时候老是跳转到 ThreadPoolExecutor 之 processWorkerExit方法,很是让人恼火,是 因为在 java.util.concurrent ...