[Leetcode] Roman to integer 罗马数字转成整数
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路:有关罗马数字的相关知识可见博客Integer to roman。从左往右遍历字符串,比较当前为和下一位的值,若是当前数字比下一位大,或者当当前数字为最后时,则加上当前数字,否则减去当前数字。这就遇到一个问题,罗马数字的字符串不是按26个字母顺序来的,如何确定大小。解决办法一:写一个函数,将罗马数字转换成整数,然后进行比较;解法二,可以使用map数据结构,将罗马数字的字母转换成对应的整数值。
方法一的代码如下:
class Solution {
public:
int romanToInt(string s)
{
int res=; //记得初始化
for(int i=;i<s.size();++i)
{
if(i==s.size()-||strToNum(s[i])>=strToNum(s[i+]))
res+=strToNum(s[i]);
else
res-=strToNum(s[i]);
}
return res;
} int strToNum(const char c)
{
int num=;
switch(c)
{
case 'M':
num=;
break;
case 'D':
num=;
break;
case 'C':
num=;
break;
case 'L':
num=;
break;
case 'X':
num=;
break;
case 'V':
num=;
break;
case 'I':
num=;
break;
default:
num=;
}
return num;
}
};
方法二:
class Solution {
public:
int romanToInt(string s) {
int res = ;
unordered_map<char, int> m{{'I', }, {'V', }, {'X', }, {'L', }, {'C', }, {'D', }, {'M', }};
for (int i = ; i < s.size(); ++i)
{ if (i == s.size() - || m[s[i]] >= m[s[i+]])
res += m[s[i]];
else
res -= m[s[i]];
}
return res;
}
};
[Leetcode] Roman to integer 罗马数字转成整数的更多相关文章
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- [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 ...
- Roman to Integer(将罗马数字转成整数)
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】Roman to Integer(罗马数字转整数)
这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...
- 013 Roman to Integer 罗马数字转整数
给定一个罗马数字,将其转换成整数. 返回的结果要求在 1 到 3999 的范围内. 详见:https://leetcode.com/problems/roman-to-integer/descript ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)
1.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range f ...
- LeetCode: Roman to Integer 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
随机推荐
- djangorestframework怎么这么好用!
一年前就已经用过restframework, 当时觉得这个只是给web框架打辅助的, 他能实现的我也都实现(可能没有那么好用, 嘿嘿) 但是我有一种东西叫做效率, 时间就是金钱, 别人造好的就直接用就 ...
- Redis面试问题
下面列出的这些其中有一些是我面试时遇到的,但是当时自己还不会,所以在网站上找了以下,然后整理出来,加强记忆 感谢码洞将这些问题整理出来: Redis有哪些数据结构? 字符串String.字典Hash. ...
- Python全栈day 02
Python全栈day 02 一.循环语句 while 用法 num = 1 while num <= 10: print(num) num += 1 # 循环打印输出1-10 while el ...
- 回形矩阵--python
def bsm(n): a = [[0]*n for x in range(n)] p = 0 q = n-1 t = 1 while p < q: for i in range(p,q): a ...
- 【动态规划】[UVA1025]A Spy in the Metro 城市里的间谍
参考:https://blog.csdn.net/NOIAu/article/details/71517440 https://blog.csdn.net/c20180630/article/deta ...
- sort()的部分用法
#include <iostream> #include <cstdio> #include <algorithm>//sort要包含的头文件 #include & ...
- kafka集群部署文档(转载)
原文链接:http://www.cnblogs.com/luotianshuai/p/5206662.html Kafka初识 1.Kafka使用背景 在我们大量使用分布式数据库.分布式计算集群的时候 ...
- windows下subversion服务器搭建
一.下载subversion服务器端和客户端软件 1.subversion下载地址:http://subversion.tigris.org/ 2.svn比较流行的客户端Tortoisesvn下载地址 ...
- CSS3实现3D球体旋转动画
html <div class="ball-box"> <div class="ball"> <div class="l ...
- 两个完整的jquery slide多方面滑动效果实例
实例1,需要引用jquery-ui.js <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...