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 ...
随机推荐
- gdb对应vc调试命令
gdb vc调试对照表: 实现功能 vc gdb 修改后编译 f7 ma ...
- poj 1035 Spell checker ( 字符串处理 )
Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16675 Accepted: 6087 De ...
- go语言基础之导入包的常用方法
1.导入包 示例: 法一 package main //导入包,必须使用,否则编译不过 import "fmt" import "os" func main() ...
- 【python】gevent学习
之前测试了stackless,感觉不太好. 不过python作为最火的脚本语言,还是吸引力难挡. python的协程方案,除了stackless,还有greenlet, 相应的事件框架也有gevent ...
- [置顶] 自娱自乐1之Linux UDC驱动(形式模板)
首先,我不是做驱动的开发人员.所以只能用自娱自乐来表示我的行为. 我不知道udc和gadget驱动是不是冷门的驱动,资料真是不多.我之前买了一本书,上面说到这些,就教你如何调试已写好的驱动.这样也可以 ...
- OpenStack 部署总结之:单节点icehouse网桥的配置
部署完icehouse,安装完实例之后.假设虚拟主机须要和外部进行通信.还须要对宿主机的网桥进行配置 宿主机的配置 改动ifcfg-em1的内容为下面内容: DEVICE=em1 ONBOOT=yes ...
- Vc++内存布局
Vc++内存布局 测试平台 Windows server 2012 R2 and visual studio 2013 professional. 本篇文章意在介绍vc++中类的内存布局方式,只是研究 ...
- C++ 11 - STL - 函数对象(Function Object) (中)
我们再来看一个复杂的例子 需求: 我们需要对集合内每个元素加上一个特定的值 代码如下: AddInt.h class AddInt { private: int theValue; // the va ...
- [Elasticsearch] 向已存在的索引中加入自己定义filter/analyzer
问题描写叙述 随着应用的不断升级,索引中的类型也会越来越多,新添加的类型中势必会使用到一些自己定义的Analyzer.可是通过_settings端点的更新API不能直接在已经存在的索引上使用. 在se ...
- Appium环境的安装与配置,Python测试脚本测试
Appium自动化测试系列1 - Appium环境的安装与配置 发表于4个月前(2015-01-27 14:34) 阅读(803) | 评论(0) 0人收藏此文章, 我要收藏 赞0 寻找 会’偷懒 ...