[LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer.
The answer is guaranteed to be within the range from 1 to 3999.
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 罗马数字转化成整数的更多相关文章
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [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 from 1 t ...
- 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 ...
- Roman to Integer(将罗马数字转成整数)
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 013 Roman to Integer 罗马数字转整数
给定一个罗马数字,将其转换成整数. 返回的结果要求在 1 到 3999 的范围内. 详见:https://leetcode.com/problems/roman-to-integer/descript ...
- 【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 ...
- LeetCode-13. Roman to Integer(罗马数字转阿拉伯数字)
1.题目描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range f ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
随机推荐
- MySQL数据库自带备份与恢复工具:MySQLdump.exe与mysql.exe
数据库的备份工作是保护数据库正常运行的关键,以下的文章主要讲述的是MySQL数据库备份的一些小妙招,我们大家都知道使用MySQL dump备份数据库的用户所需要的权限相对而言还是比较小的,只需要sel ...
- 第十七篇:使用窗口的cache属性加速SOUI的渲染
内容渲染速度是决定一个UI成败的关键.无论UI做得多华丽,没有速度都没有意义. 在MFC,WTL等开发框架下,每个控件都是一个窗口,窗口只需要画前景,背景.因为窗口之间的内容不需要做混合,一个子窗口的 ...
- 在windows系统的文件右键菜单中增加“命令提示符”
本实用小工具能够在windows系统的文件右键菜单中增加“命令提示符”,方便快速进入制定文件的命令提示窗口,避免逐层输入或复制文件夹路径,极其实用. 工具下载地址如下:360云盘(访问密码:5b71) ...
- 4.0 和4.5 app 和generic,xaml的问题
4.0里面不支持Generic.xaml里面 <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source=& ...
- connot resolve symbol R
出现这个原因大都是layout里的xml文件出错,就不会自动生成R文件
- Arduino101学习笔记(八)—— 函数库
/*********最小值*********/ min() //实现:#define min(a,b) ((a)<(b)?(a):(b)) /*********最大值*********/ max ...
- Java学习笔记(三)——运算符
一.运算符: 1.分类: 2.java中的运算符 (1)其中,++在左,表示先加了再用,++在右,表示先用了再加. (2)% 用来求余数,也称为"取模运算符" 3.赋值运算符 4. ...
- 分布式服务框架 Zookeeper -- 管理分布式环境中的数据
转自:http://www.ibm.com/developerworks/cn/opensource/os-cn-zookeeper/index.html Zookeeper 分布式服务框架是 Apa ...
- 【jQuery 使用】 利用jQuery.prop("outerHTML")获取包含自身在内的HTML元素的HTML代码
jQuery.html() 是获取当前节点下的html代码,并不包含当前节点本身的代码,然而我们有时候的确需要,可以通过jQuery.prop("outerHTML")的方式设置. ...
- AndroidTips:解决Dialog全屏显示以及Dialog显示自动弹出输入法
继承实现一个dialog,并在onCreate里面做处理. @Override protected void onCreate(Bundle savedInstanceState) { s ...