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 ...
随机推荐
- jquery省市选择案例
1.代码实例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- 如何设置Win7不待机 Win7进入待机状态会断网的解决方法
电脑一旦进入待机状态后,会断网,应用将停止运行,因此需要设置电脑不待机来解决,这种情况需要挂一些游戏或者下载应用的时非常实用,下面就与大家分享下电脑不待机的设置方法,感兴趣的朋友可以参考下 有时候我们 ...
- 【Scroller】scrollTo scrollBy startScroll computeScroll 自定义ViewPage 简介 示例
简介 android.widget.Scroller是用于模拟scrolling行为,它是scrolling行为的一个帮助类.我们通常通过它的 startScroll 函数来设置一个 scrollin ...
- qt study 元对象,属性和反射编程
所谓反射,就是指对象成员的自我检查,使用反射编程(reflective programming),就可以编写出通用的操作,可以对具有不同结构的类进行操作. QMetaObject 元对象模式,描述一个 ...
- Android -- 启动模式
Android的启动模式分为四种: standard 模式启动模式,每次激活Activity时都会创建Activity,并放入任务栈中. singleTop 如果在任务的栈顶正好存在该Activity ...
- MYSQL 命令行工具自动登录的方法
MYSQL 命令行工具自动登录的方法 1. 需求提出 由于在linux 环境下,经常需要使用mysql(command-line tool) 终端连接到MYSQL DB服务. 其中大致的语法如下: m ...
- [React] Ensure all React useEffect Effects Run Synchronously in Tests with react-testing-library
Thanks to react-testing-library our tests are free of implementation details, so when we refactor co ...
- Activiti 流程启动及节点流转源代码分析
作者:jiankunking 出处:http://blog.csdn.net/jiankunking 本文主要是以activiti-study中的xiaomage.xml流程图为例进行跟踪分析 详细的 ...
- 更简单更全的material design状态栏
从实际使用须要出发,以最简单的方式实现了几种类型的MD状态栏. (重点在fitsSystemWindows的使用) 0,使用前提 Theme.AppCompat.Light.DarkActionBar ...
- 【转】四大机器学习降维算法:PCA、LDA、LLE、Laplacian Eigenmaps
最近在找降维的解决方案中,发现了下面的思路,后面可以按照这思路进行尝试下: 链接:http://www.36dsj.com/archives/26723 引言 机器学习领域中所谓的降维就是指采用某种映 ...