013 Roman to Integer 罗马数字转整数
给定一个罗马数字,将其转换成整数。
返回的结果要求在 1 到 3999 的范围内。
详见:https://leetcode.com/problems/roman-to-integer/description/
class Solution {
public:
int romanToInt(string s) {
int res=0;
map<char, int> m{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
for(int i=0;i<s.size();++i)
{
if(i==s.size()-1||m[s[i+1]]<=m[s[i]])
res+=m[s[i]];
else
res-=m[s[i]];
}
return res;
}
};
参考:http://www.cnblogs.com/grandyang/p/4120857.html
013 Roman to Integer 罗马数字转整数的更多相关文章
- LeetCode 13 Roman to Integer(罗马数字转为整数)
题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description int toNumber(char ch) { switc ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- [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】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 ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- LeetCode--No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- [Leetcode] 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 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- C++STL库中map容器常用应用
#include<iostream> #include<cstdio> #include<map> //按键值大小构成二叉搜索树 using namespace s ...
- Linux keepalived与lvs的深入分析
一)概述 在本篇文章里,我们会涉及两部份内容,一个是LVS,另一个则是keepalived. 即我们用LVS和keepalived实现了负载均衡及高可用的服务器. LVS有实现三种IP负载均衡技术 ...
- Nmon工具的使用以及通过nmon_analyse生成分析报表
在我们监控我们的操作系统的时候如果可以把各个硬件的监控信息生成形象化的分析报表图对于我们来说是件太好的事情了,而通过ibm的nom和nmon_analyser两者的结合完全可以实现我们的要求.首先对n ...
- PHP7三元运算符 ?? 和 ?: 的区别
1. (expr1) ?? (expr2) 是 PHP7才有的功能,等同于: isset(expr1) ? expr1 : expr2 ; 2.(expr1) ?: (expr2) 是PHP5.3才 ...
- BMFont使用图片自定义字体(无需字体文件)
网上搜BMFont做字体,很多都是从一个字体文件读取,然后选择需要的字,然后保存成图片文字,这个对于一般的文字的确很实用,因为Unity本身不支持中文,所以只好这样了. 但是做过游戏的都知道,策划总是 ...
- 【msyql_获取时间的前后几天函数date_sub】
select now()-- 2017-05-16 16:48:02select curdate() -- 2017-05-16 select curdate() + 1 -- 20170517 s ...
- 大数据实习之spark
Apache Spark是一个围绕速度.易用性和复杂分析构建的大数据处理框架. 与 Hadoop 和 Storm 等其他大数据和 MapReduce 技术相比,Spark 有如下优势. 首先,Spar ...
- 微信小程序open-data获取用户的信息样式设置
效果图 wxml代码 <view class="userinfo"> <!-- 用户头像 --> <view class="userinfo ...
- CODING 告诉你硅谷的研发项目管理之道(4)
写在前面 优秀的项目管理者是怎么工作的,如何帮助研发团队高效工作?一直是 CODING 关注的重要话题,我们不断地打磨 CODING 研发系统来让开发更简单.近期我们精心挑选了几篇硅谷科技公司研发管理 ...
- foreach 加 &