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 ...
随机推荐
- 《Hadoop应用开发技术详解》
<Hadoop应用开发技术详解> 基本信息 作者: 刘刚 丛书名: 大数据技术丛书 出版社:机械工业出版社 ISBN:9787111452447 上架时间:2014-1-10 出版日期:2 ...
- 将Maven2项目转为MyEclipse项目
现在项目中,大家开始用jetty.它不用像在MyEclipse中使用Tomcat那样要部署,也不用像在Tomcat中那样,要把应用都放到webapp文件夹下.jetty可以直接用你的项目的目录结构. ...
- [Todo] Nodejs学习及Spider实验(包括php入门学习、React入门学习)
/Users/baidu/Documents/Data/Interview/Web-Server开发 深入浅出Node.js-f46c http://blog.csdn.net/u012273376/ ...
- Linux下解决 id_rsa 权限不够
问题 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: UNPROTECTED PRIVAT ...
- SQL Server 2012不支持Microsoft Visual Studio Test Controller 2010
折腾了一个上午, 发现Test Controller怎么都连不上SQL. 能尝试的都尝试了, 觉得应该看看是不是有不支持的问题. 找到了这篇. TFS 2010 will not support ...
- JavaScript中的单引号和双引号解决
在使用JavaScript显示消息或者传递字符数据的时候,经常会碰到数据中夹杂单引号(')或者双引号("),这种语句往往会造成JavaScript报错.对此一般采用/'或者/"的解 ...
- CSS_LESS 语法/函数详解
嵌套规则 01 #header { color: black; }#header .navigation { font-size: 12px; 02 }#header .logo { 03 w ...
- WebSocket原理分析
Web应用的通信过程通常是客户端通过浏览器发出一个请求,服务器端接收请求后进行处理并返回结果给客户端,客户端浏览器将信息呈现.这种机制对于信息变化不是特别频繁的应用可以良好支撑,但对于实时要求高.海量 ...
- Jfinal极速开发微信系列教程(二)--------------让微信公众平台通过80端口访问本机
概述: 微信公众平台要成为开发者,需要填写接口配置信息中的“URL”和“Token”这两项(参见:http://mp.weixin.qq.com/wiki/index.php?title=%E6%8E ...
- RaceWeb介绍(7):由500强公司数据高速生成百度地图——生成坐标字段及坐标数据
接上篇. 一.生成X坐标.Y坐标两个字段. 我们须要为每一个公司建立X坐标和Y坐标字段,用来保存XY坐标. 既然为了突出"快",这一步就有程序来完毕吧. 右键单击"世界5 ...