LeetCode——13. Roman to Integer
一.题目链接:https://leetcode.com/problems/roman-to-integer/
二.题目大意:
给定一个罗马数字,返回它的整数形式。
三.题解:
这道题与12题恰好相反,关键之处在于罗马数字的组成规律。首先,弄清楚每个罗马字母对应的数字大小:

实质上,对于一个罗马数字,只需遍历它的每个字母,然后将字母对应的数字逐一相加即可,但是如果后一个字母对应的数字比相邻的前一个字母对应的数字大的话,那么这两个字母对应的数值实际上是大的数字减去小的数字,而不是两个数字的和。举个例子:例如MCM,它有两个需要特别注意的地方:
1.CM对应的数值实际上是M-C,即1000-100 = 900.
2.由于在遍历判断的时候,C会多加上一次,所以在遇到类似"CM"的情况时,需要用M-2*C,把多加的那个C减去。
代码如下:
class Solution {
public:
int romanToInt(string s) {
int len = s.size();
unordered_map<char,int> mp;
mp.insert(pair<char,int>('I',1));
mp.insert(pair<char,int>('V',5));
mp.insert(pair<char,int>('X',10));
mp.insert(pair<char,int>('L',50));
mp.insert(pair<char,int>('C',100));
mp.insert(pair<char,int>('D',500));
mp.insert(pair<char,int>('M',1000));
int val = mp.find(s[0])->second;
for(int i = 1; i < len; i++)
{
if(mp.find(s[i])->second > mp.find(s[i - 1])->second)
{
val += (mp.find(s[i])->second - 2 * mp.find(s[i - 1])->second);
}
else
{
val += mp.find(s[i])->second;
}
}
return val;
}
};
此处我是从下标1处开始遍历的,由于是判断s[i] >s[i-1],所以会出现多加一次C的情况,所以才会减去2*C。
LeetCode——13. Roman to Integer的更多相关文章
- 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 ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [leetcode]13. Roman to Integer罗马数字转整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 13. Roman to Integer ☆☆
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [leetcode] 13. Roman to Integer
如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...
随机推荐
- python import 其他 package的模块
https://blog.csdn.net/luo123n/article/details/49849649 http://blog.habnab.it/blog/2013/07/21/python- ...
- CSS使用方法
CSS行内样式: 在开始标签内添加style样式属性 如:<p style="color:red;">内容</p> CSS内部样式: 内部样式(嵌入样式), ...
- 超链接(空链接-target-title属性)
空链接:<a href="#">内容</a> 超链接标签: <a href=" " >内容</a> 属性: hr ...
- Mac电脑使用:您的安全性偏好设置仅允许安装来自App Store和被认可的开发者的应用(解决方法)
在10ms加速器上下载了MAC客户端ShadowsocksX,打开软件,提示打不开,因为来自身份不明的开发者. 以下为解决办法: Step1:打开dock栏里面的“系统偏好设置” Step2:在系统偏 ...
- IE8的兼容问题
1: rgba失效的问题: 在添加rgba的类名内加上:filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#7f00000 ...
- Redis 当成数据库在使用和可靠的分布式锁,Redlock 真的可行么?
怎样做可靠的分布式锁,Redlock 真的可行么? https://martin.kleppmann.com/2016/02/08/how-to-do-distributed-locking.html ...
- spring管理
spring管理 SqlMapConfig.xml: <?xml version="1.0" encoding="UTF-8"?> <bean ...
- webapi core2.1 IdentityServer4.EntityFramework Core进行配置和操作数据
https://identityserver4.readthedocs.io/en/release/quickstarts/8_entity_framework.html 此连接的实践 vscode ...
- 【HDOJ1018】【大数阶乘位数】【斯特林公式】
http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others) ...
- LeetCode - Cut Off Trees for Golf Event
You are asked to cut off trees in a forest for a golf event. The forest is represented as a non-nega ...