Given a roman numeral, convert it to an integer.

The answer is guaranteed to be within the range from 1 to 3999.

Have you met this question in a real interview?

Yes
Example

IV -> 4

XII -> 12

XXI -> 21

XCIX -> 99

LeetCode的原题,请参见我之前的博客Roman to Integer

解法一:

class Solution {
public:
/**
* @param s Roman representation
* @return an integer
*/
int romanToInt(string& s) {
int res = ;
unordered_map<char, int> m{{'I', }, {'V', }, {'X', }, {'L', }, {'C', }, {'D', }, {'M', }};
for (int i = ; i < s.size(); ++i) {
if (i == s.size() - || m[s[i]] >= m[s[i + ]]) res += m[s[i]];
else res -= m[s[i]];
}
return res;
}
};

解法二:

class Solution {
public:
/**
* @param s Roman representation
* @return an integer
*/
int romanToInt(string& s) {
int res = ;
unordered_map<char, int> m{{'I', }, {'V', }, {'X', }, {'L', }, {'C', }, {'D', }, {'M', }};
for (int i = ; i < s.size(); ++i) {
if (i == ) res += m[s[i]];
else {
if (m[s[i]] > m[s[i - ]]) res += m[s[i]] - * m[s[i - ]];
else res += m[s[i]];
}
}
return res;
}
};

[LintCode] Roman to Integer 罗马数字转化成整数的更多相关文章

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

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

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

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

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

  4. 13. Roman to Integer 罗马数字转化为阿拉伯数字(indexOf ()和 toCharArray())easy

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  5. Roman to Integer(将罗马数字转成整数)

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

  6. 013 Roman to Integer 罗马数字转整数

    给定一个罗马数字,将其转换成整数. 返回的结果要求在 1 到 3999 的范围内. 详见:https://leetcode.com/problems/roman-to-integer/descript ...

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

  8. LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)

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

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

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

随机推荐

  1. PMP 第七章 项目成本管理

    估算成本 制定预算 控制成本 1.成本管理计划的内容和目的是什么?     包括对成本进行估算 预算和控制的各过程,从而确保项目在批准的预算内完工. 2.直接成本.间接成本.可变成本.固定成本.质量成 ...

  2. windbg的高级玩法

    详见附件  jpg改zip

  3. Google自己的下拉刷新组件SwipeRefreshLayout

    SwipeRefreshLayout SwipeRefreshLayout字面意思就是下拉刷新的布局,继承自ViewGroup,在support v4兼容包下,但必须把你的support librar ...

  4. Redis学习笔记(1) Redis介绍及基础

    1. Redis的特性 (1) 存储结构 Redis(Remote Dictionary Server,远程字典服务器)是以字典结构存储数据,并允许其他应用通过TCP协议读写字典中的内容.Redis支 ...

  5. SQL作业的操作全

    --定义创建作业 转自http://hi.baidu.com/procedure/blog/item/7f959fb10d76f95d092302dd.html DECLARE @jobid uniq ...

  6. PHP之echo/print

    1.PHP中有两个基本的输出方式:echo和print: 2.echo和print的区别: **echo:可以输出一个或多个字符串: **print:只允许输出一个字符串,返回值总为1: 3.echo ...

  7. 《DSP using MATLAB》示例Example4.2

  8. DSP using MATLAB 示例Example3.9

    用到的性质 上代码: n = 0:100; x = cos(pi*n/2); k = -100:100; w = (pi/100)*k; % freqency between -pi and +pi ...

  9. js总结1

  10. iOS 安装Cocoapods以及安装第三方库的操作流程

    安装cocoapods的流程: 1.打开终端,输入:  sudo gem update —system 2.输入密码,稍等 3.gem sources --remove https://rubygem ...