Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

下面是百度得到的关于罗马数的解释:

我的代码:

 class Solution {
public:
int romanToInt(string s) {
map<char,int> conversion;
conversion.insert(make_pair('I',));
conversion.insert(make_pair('V',));
conversion.insert(make_pair('X',));
conversion.insert(make_pair('L',));
conversion.insert(make_pair('C',));
conversion.insert(make_pair('D',));
conversion.insert(make_pair('M',));
int ans=;
for(unsigned i=; i<s.size(); i++)
{
if(s[i]=='V' || s[i]=='L' || s[i]=='D') //不能把基本数字V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目
ans += conversion[s[i]];
else //基本数字Ⅰ、X 、C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个;放在大数的左边只能用一个
{
if(i<s.size()- && conversion[s[i]]<conversion[s[i+]])
{
ans += conversion[s[i+]]-conversion[s[i]];
i++;
}
else
ans += conversion[s[i]];
}
}
return ans;
}
};

[LeetCode OJ] Roman to Integer的更多相关文章

  1. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...

  2. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  3. 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 ,即 ...

  4. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  5. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  6. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

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

  8. 【leetcode】Roman to Integer

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

  9. 【JAVA、C++】LeetCode 013 Roman to Integer

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

随机推荐

  1. Object-c 基础总结

    1,nil,NULL,NSNull:nil用来给对象附值,object—c里允许对象为空,空对象也可以接受消息.但是不允许指针为空,NULL是给任何指针附值的.所以NULL只在C或C++里才用.NSN ...

  2. 27 Best Free Eclipse Plug-ins for Java Developer to be Productive

    Eclipse offers an integrated development environment having an extensible plug-in system. This enabl ...

  3. PTA 06-图3 六度空间 (30分)

    "六度空间"理论又称作"六度分隔(Six Degrees of Separation)"理论.这个理论可以通俗地阐述为:"你和任何一个陌生人之间所间隔 ...

  4. 关于一次Weblogic活动线程的问题处理

    Weblogic控制台监控发现 环境>>服务器>>你的服务器>>监控>>线程 中活动执行线程竟然是2000多.同一套系统在另一套平台上,并且访问的人不少 ...

  5. AppiumLibrary实用函数介绍

    1. 点击按钮: Click Button index_or_name Click button 实例:Click Button index=0 作者通过实验发现在安卓手机应用测试中,name这个属性 ...

  6. 新安装XAMPP,phpMyAdmin错误:#1045 - Access denied for user 'root'@'localhost' (using password: NO)

    错误如下: 打开D:\Program Files\xampp\phpMyAdmin(你的xampp的安装目录下的phpMyAdmin目录)目录下的config.inc.php文件 将 改为 然后,错误 ...

  7. Binary image

    http://www.uio.no/studier/emner/matnat/ifi/INF3300/h06/undervisningsmateriale/week-36-2006-solution. ...

  8. hibernate中持久化对象的生命周期(三态:自由态,持久态,游离态 之间的转换)

    三态的基本概念: 1,  暂时状态(Transient):也叫自由态,仅仅存在于内存中,而在数据库中没有对应数据.用new创建的对象,它没有持久化,没有处于Session中,处于此状态的对象叫暂时对象 ...

  9. BringWindowToTop(), SetForegroundWindow(), SetActiveWindow()

    1. SetActiveWindow() 原型: <span style="font-size:14px;">CWnd* SetActiveWindow(); HWND ...

  10. json xmpp

    https://github.com/lamfire/jspp http://blog.csdn.net/nomousewch/article/category/823687 http://my.os ...