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 ...
随机推荐
- angular的点击添加
首先是在js里面我们可以用clone来点击添加一些东西比如列表或者其他的div之类的,但是在angular里面怎么实现点击添加呢? 类似这种: 这样就尴尬了,最少我这样的菜鸟是不知道怎么去写的,网上好 ...
- POJ3070 Fibonacci(矩阵快速幂加速递推)【模板题】
题目链接:传送门 题目大意: 求斐波那契数列第n项F(n). (F(0) = 0, F(1) = 1, 0 ≤ n ≤ 109) 思路: 用矩阵乘法加速递推. 算法竞赛进阶指南的模板: #includ ...
- nginx的安装 、Nginx默认虚拟主机、nginx用户认证、nginx 域名重定向
1.nginx:官网:www.nginx.org 下载:wget -c http://nginx.org/download/nginx-1.14.0.tar.gz解压:tar -zxvf nginx ...
- linux cron计划任务、chkconfig 命令、systemd命令、unit 相关、target 相关
1.设置说明位置 : cat /etc/crontab # Example of job definition:# .---------------- minute (0 - 59)# | .---- ...
- 质因子分解(Pollard_Rho法)
LL Pollard_Rho(LL n, LL c) { LL x, y, d; LL i = , k = ; x = y = rand() % n; do { i++; d = gcd(n + y ...
- java知识整理
整理一下Java知识点. 一.final finally finalize区别 1.final 修饰符(关键字).被final修饰的类,不能再派生出新的子类,不能作为父类而被子类继承.因此一个类不能既 ...
- 解决ERROR - unregister mbean error javax.management.InstanceNotFoundException: com.alibaba.druid:type=
https://blog.csdn.net/chengsi101/article/details/72627062 https://www.cnblogs.com/gradven/p/6323195. ...
- CF使用TGP下载后,分卷文件损坏的解决方法
首先从游戏的列表删除游戏(安装失败出现分卷文件损坏的游戏) 然后进入游戏重新,继续找到该游戏(安装失败的游戏) 点击下载游戏!不会重新下载的,之后下载一些失败的文件,不会花费多少时间,慢慢等待即可 之 ...
- debian 配置linuxptp 软件时间戳
编程之路刚刚开始,错误难免,希望大家能够指出. ntp,ptp,ntp,ptp 本文只说软件时间戳 先上几个推荐的网址,可以更好的了解ptp: https://docs.fedoraproject.o ...
- zsh:no matches found 问题解决
解决方法: ~/.zshrc 文件加入: setopt no_nomatch 之后,更新配置 source ~/.zshrc