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 ...
随机推荐
- ps教程
http://www.16xx8.com/photoshop/xinshoujiaocheng/
- alpha冲刺(1/10)(作废)
前言 队名:旅法师 作业链接 队长博客 燃尽图 会议 站立式会议照片 会议内容 陈晓彬(组长) 昨日进展: 召开会议 安排任务 博客撰写 问题困扰: 没有做项目经理的经验,在沟通方面专业知识不够. 心 ...
- grep 的学习 正则
grep 命令: grep "name" /path/file_name 从file_name文件中中查找 name 字符 grep -c "name ...
- UE4 IOS 开发之传感器输入
Iphone的传感器包括陀螺仪.加速计等. UE4提供了4个按键来收集这些传感器的3维数据.具体位置:ProjectSettings->Input. 其中Tilt数据可以反映Iphone目前的物 ...
- 【java编程】ServiceLoader使用看这一篇就够了
转载:https://www.jianshu.com/p/7601ba434ff4 想必大家多多少少听过spi,具体的解释我就不多说了.但是它具体是怎么实现的呢?它的原理是什么呢?下面我就围绕这两个问 ...
- 把oracle数据库恢复到某个时间点或者某个scn
alter session set nls_date_format='yyyymmdd hh24:mi:ss'; select sysdate from dual; conn dbauser/1234 ...
- Java开发培训基础知识解析之反射机制
Java是老牌编程语言,是当前应用最广泛的编程语言之一.想要学习Java你就一定要掌握Java基础知识,而反射对于初学Java的人来说绝对是非常重要的知识点.什么是反射?如何理解反射机制?如何使用反射 ...
- hasura graphql-engine v1.0.0-alpha30 版本新功能介绍
hasura graphql-engine v1.0.0-alpha30 发布了,以下为一些变动的简单说明 破坏性的变动 order_by 中的desc 从 desc nulls last 修改为 d ...
- nginx+php windows安装配置
https://blog.csdn.net/zjiang1994/article/details/72876193 https://blog.csdn.net/bruce_wang_janet/art ...
- Python3:sorted()函数及列表中的sort()函数
一.sort,sorted函数介绍: Sort函数是list列表中的函数,而sorted可以对list或者iterator进行排序. 下面我们使用help来查看他们的用法及功能: sort: ...