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 ...
随机推荐
- transition的用法以及animation的用法
http://www.cnblogs.com/xiaohuochai/p/5347930.html transiton: 过渡属性 过渡所需要时间 过渡动画函数 过渡延迟时间: 触发过渡 单纯的代码不 ...
- 小米4c刷LineageOS
注意,本文仅限于小米4c,其他手机仅可参考步骤.如下rom,su,gapps包的下载都是小米4c的,深刷miflash也仅适用于小米手机.准备工作:请自行备份好手机内的个人资料. 电脑环境,usb驱动 ...
- c# 敏捷3 连接,批量处理,分页
class Program { public class post { public int id { get; set; } public string name { get; set; } pub ...
- js中数组的去重
第一种方式: var ss=['小红','小花','小兰','小花'] var uu=[] for(var i=0;i<ss.length;i++){ if(uu.indexOf(ss[i])= ...
- Vue 1组件的使用
在components新建一个vue组件: <template> <div> <h1>{{ msg }}</h1> </div> </ ...
- diskcache
DiskCache: Disk Backed Cache DiskCache is an Apache2 licensed disk and file backed cache library, wr ...
- pycharm远程调试配置
目录: 安装pycharm 配置pycharm远程调试 使用测试 一.安装pycharm(略) 二.配置pycharm远程调试 1.菜单--->Tools--->Deployment--- ...
- 在Android上运行Java和C程序
在linux上运行java程序,直接用javac编译,再用java启动虚拟机运行就行了.但是在Android上,由于虚拟机和pc端的不同,所以操作方法也是不一样的.下面介绍Android上运行Hell ...
- pipenv 方便的python 开发工作流工具
pipenv 将 composer.bundler.npm.yarn.cargo 等比较方便的包管理工具添加到了python 语言中,可以 帮助我们自动的管理virtualenv ,同时可以方便的从p ...
- minio 对于压缩的处理
我们可以简单的配置就可以让minio 支持数据压缩了,这个对于减少带宽的请求,以及web 端的优化很有意义 配置说明 配置文件 "compress": { "enable ...