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. Good Numbers

    Problem Description If we sum up every digit of a number and the result can be exactly divided by 10 ...

  2. UVa 10054 The Necklace BFS+建模欧拉回路

    算法指南 主要就是建立欧拉回路 #include <stdio.h> #include <string.h> #include <iostream> #includ ...

  3. java解惑

    java对转义字符没有提供任何特殊处理.编译器在将程序解析成各种符号之前,先将 Unicode 转义字符转换成为它们所表示的字符[JLS 3.2] 阅读笔记

  4. (DT系列六)devicetree中数据和 struct device有什么关系

    devicetree中数据和structdevice有什么关系 总体来说,devicetree与structdevice的关系应该还是在其生成platformdevice的时候,一直传递的struct ...

  5. 微信开发第6章 通过accesstoken获取用户粉丝列表

    上一章我们讲解到open_id获取用户基本信息,那么open_id哪儿来的呢?就是粉丝列表中可以看到的.本次讲解如何获取粉丝列表. 获取粉丝列表 可以查看文档 http://mp.weixin.qq. ...

  6. ios 记录支付宝集成遇到的坑及解决方法

    今天项目中要开始动手集成支付宝支付,在此小结一下.(目前新版的支付宝SDK有较大改版,去集成还需要自己去开发平台详细的按照集成步骤来完成https://doc.open.alipay.com/docs ...

  7. rails + mongoid 使用

    1. 测试环境 2. 创建工程 rails new mongoid_app --skip-active-record --skip-test-unit --skip-bundle 3. 修改gemfi ...

  8. 深入了解Angularjs指令中的ngModel

    关于AngularJs的指令的知识学习,请参考... 这次我们接上次没讲完的知识继续. 前端人员在设计表单逻辑时, 在大部分情况下,我们需要为表单定义很多指令, 比如比较两个input内的值是否相同, ...

  9. 文件读写操作(含SDCard的读写)

    1.在AndroidManifest文件下添加SDCard的读写权限 <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name ...

  10. linux 进程综合指令

    1. 查询当前机器运行的进程总数: ps -ef | wc -l ps -ef | grep httpd | wc -l 2. ulimit命令 表 1. ulimit 参数说明 选项 [option ...