给定一个罗马数字,将其转换成整数。

返回的结果要求在 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 罗马数字转整数的更多相关文章

  1. LeetCode 13 Roman to Integer(罗马数字转为整数)

    题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description   int toNumber(char ch) { switc ...

  2. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  3. [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 ...

  4. 【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 ...

  5. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  6. No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

  7. LeetCode--No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

  8. [Leetcode] Roman to integer 罗马数字转成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  9. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. mysql查询语句例题

    1.一条SQL语句查询两表中两个字段 首先描述问题,student表中有字段startID,endID.garde表中的ID需要对应student表中的startID或者student表中的endID ...

  2. 转载:Android应用的自动更新模块

    软件的自动更新一般都与Splash界面绑定在一起, 由于需要维护的软件界面很复杂, 一个Activity中嵌入ViewPager, 并且逻辑比较复杂, 索性重新写一个Activity, 现在的软件都很 ...

  3. USACO-Greedy Gift Givers(贪婪的送礼者)-Section1.2<2>

    [英文原题] Greedy Gift Givers A group of NP (2 ≤ NP ≤ 10) uniquely named friends has decided to exchange ...

  4. day18-事务与连接池 6.事务隔离级别与解决问题

    开两个cmd窗口,相当于两个事务. read-uncommitted这种级别是解决不了任何问题的,它什么情况都能出现.刚才演示了脏读,再演示就出现了不可重复读. read-committed隔离级别能 ...

  5. call apply bind 的区别

    1.call和apply都是对函数的直接调用,而bind方法返回的仍然是一个函数,因此后面还需要()来进行调用才可以 var xw={ name: "小王", gender: &q ...

  6. oracle上课 学习2 oracle 游标 存储过程 有用

    1.1. 训练描述 使用游标,打印emp中20号部门的所有员工的信息 操作步骤答案 declare cursor c_emp  is select * from emp where deptno=10 ...

  7. 阶段3-团队合作\项目-网络安全传输系统\sprint1-传输子系统设计\第3课-加密传输优化

    对之前的传输系统进行加密,使之成为加密的网络传输系统 客户端编程模型 通过以上模型对传统的TCP传输模型进行优化 首先完成初始化工作,它是要在创建socket之前完成 主要是以上四个函数的实现,那么这 ...

  8. 6.5 Ubuntu中安装搜狗输入法

    传统的方式:http://www.cnblogs.com/zlslch/p/6943318.html 最简单的方式:

  9. java虚拟机内存

    -Xmx10240m:代表最大堆  -Xms10240m:代表最小堆  -Xmn5120m:代表新生代  -XXSurvivorRatio=3:代表Eden:Survivor = 3    根据Gen ...

  10. jquery的命名空间

    function A( event ){    alert( 'A' );}function B( event ){    alert( 'B' );}function C( event ){    ...