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

返回的结果要求在 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. CSS之EM相对单位

    之前以为em单位只是在font-size中起到继承作用, 后来慢慢觉得,继承,应该是在几乎所有样式中都可以是实现的,比如height,width,border... 今天才简单测试了下,果真是可以实现 ...

  2. 洛谷【P2005】A/B Problem II

    题目传送门:https://www.luogu.org/problemnew/show/P2005 高精除低精:https://www.cnblogs.com/AKMer/p/9724556.html ...

  3. Poj 1860 Currency Exchange(Bellman-Ford,SPFA解单源最短路径问题)

    一.题意 有多个货币交易点,每个只能互换两种货币,兑换的汇率不同,并收取相应的手续费.有N种货币,假定你拥有第S中,数量为V,有M个兑换点.问你能不能通过兑换操作使你最后拥有的S币比起始的时候多. 二 ...

  4. vijos1782:借教室

    描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借教室的信息,我们自然希望 ...

  5. spring下,druid,c3p0,proxool,dbcp四个数据连接池的使用和配置

    由于那天Oracle的数据连接是只能使用dbcp的数据库连接池才连接上了,所以决定试一下当下所有得数据库连接池连接orcale和mysql,先上代码 配置文件的代码 #================ ...

  6. Python:生成器表达式

    转于:http://www.cnblogs.com/liu-shuai/p/6098218.html 博主:刘-帅 简介: 生成器表达式并不真正的创建数字列表,而是返回一个生成器对象,此对象在每次计算 ...

  7. python 基础 字符串格式化

    print "hello %s %s" % ('wd','pc') c风格 print "hello {1} {0}".format("wd" ...

  8. shell入门-grep-3-egrep

    grep -E == egrep [root@wangshaojun ~]# grep --color 'r\?o' 1.txt == egrep --color 'r?o' 1.txt ^C[roo ...

  9. CentOS 7 搭建 LAMP

    一.安装httpd 1.yum install httpd -y 2.启动服务:systemctl start httpd 3.设置开机启动:systemctl enable 二.安装mariadb ...

  10. mysql主从服务器复制操作

    master主机ip:192.168.1.19  用户名sunzy 密码123456slave主机ip:192.168.1.20 1.配置master1)接下来对master进行配置,包括打开二进制日 ...