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 ...
随机推荐
- 九度OJ-1131-合唱排队-双向递增子序列
题目1131:合唱队形 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4948 解决:1570 题目描述: N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交 ...
- BFS深度优先搜索 炸弹人
题面:一个人在一个坐标放炸弹,请问可以可以杀死的敌人数目最大是,并且输出该点的坐标 G代表敌人 .代表该位置可以走 "#"代表该位置存在障碍物 并且防止炸弹的蔓13 13 3 3 ...
- string的方法find
官方解释:find(sub[, start[, end]]) Return the lowest index in the string where substring sub is found wi ...
- day29akka
PS:AKKA之前要实现并发编程,通常要借用netty框架,现在如果又要高并发又要分布式就使用akka框架这个akka在客户端和服务端每一端都相当于一个actor,尤其是服务端需要一个总管进行管理 P ...
- Servlet拓展
一. 概念 1.Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务 连接器,用Java编写的服务器端程序,主要功能在于交互式地浏览和修改数据, ...
- 数学 它的内容,方法和意义 第三卷 (A. D. 亚历山大洛夫 著)
第十五章 实变数函数论 1. 绪论 2. 集合论 3. 实数 4. 点集 5. 集合的测度 6. 勒贝格积分 第十六章 线性代数 1. 线性代数的对象和它的工具 2. 线性空间 3. 线性方程组 4. ...
- Tanks!Tutorial 学习
using UnityEngine; namespace Complete { public class CameraControl : MonoBehaviour { /// <summary ...
- Yuan先生的博客网址
1 Web应用 https://www.cnblogs.com/yuanchenqi/articles/8869302.html 2 http协议 https://www.cnblogs.com/y ...
- 短信验证登陆-中国网建提供的SMS短信平台
一.JAVA发送手机短信常见的有三种方式(如下所列): 使用webservice接口发送手机短信,这个可以使用sina提供的webservice进行发送,但是需要进行注册 使用短信mao的方式进行短信 ...
- How tbb proxy works