[LeetCode OJ] Roman to Integer
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的更多相关文章
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- 【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 ...
- 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 ,即 ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- [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
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- 【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 ...
随机推荐
- IO流的应用————小型资源管理器
小型资源管理器 private void LoadTreeView() { DirectoryInfo dir = new DirectoryInfo(@"E:\"); Direc ...
- Android核心基础(手机卫士的一个知识点总结)
注意:有些功能是需要权限的,在这里并没有写出来,在程序运行中,根据程序报的错误,添加相应的权限即可,里面的具体里面可能有一些小细节,没有明确的写出来,具体的需要在程序中自己调试,解决. 这个总结涵盖了 ...
- C语言练习题_邮票组合
背景: 我们寄信都要贴邮票,在邮局有一些小面值的邮票,通过这些小面值邮票中的一张或几张的组合,可以满足不同邮件的不同的邮资. 现在,邮局有4种不同面值的邮票.在每个信封上最 ...
- EJB 总结学习(1)
总结1: 以下面这行代码为例: PersonDaoBeanRemote pdb = (PersonDaoBeanRemote)ctx.lookup("PersonDaoBean/remote ...
- 黑马程序员_<<GUI(图形用户界面)--------1>>
--------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1. GUI图形用户界面 1.简述 Gra ...
- jQuery 的属性操作方法
jQuery 属性操作方法 下面列出的这些方法获得或设置元素的 DOM 属性. 这些方法对于 XML 文档和 HTML 文档均是适用的,除了:html(). 方法 描述 addClass() 向匹配的 ...
- Oracle数据库中将一个数据库中一张表的数据导入到另外一张表
INSERT INTO DBTHNEW.L_MEMBER_ROLE_REL SELECT *FROM DBTH.L_MEMBER_ROLE_REL
- Android 在广播接收器中弹出对话框
特别需要注意的几点如下: 需要设置AlertDialog的类型 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT 2. 需要声明Window弹框的权限 < ...
- php如何修改SESSION的生存时间
如何修改SESSION的生存时间 我们来手动设置 Session 的生存期: <?phpsession_start(); // 保存一天 $lifeTime = 24 * 3600; setco ...
- centos中使用python遇到的几个问题
用python搞了一个从excel中读取cobbler节点信息并加入cobbler中的脚本,运行的过程中出了不少问题,这里记录下来,方便日后查找! 一.yum install python,我通过这个 ...