[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 ...
随机推荐
- 【译】安装Sonar要求
本文仅为本人看sonar官方文档时,因其为英文,故简单整理翻译[英语不好,见谅!] http://docs.sonarqube.org/display/SONAR/Requirements 目录 ...
- usb设备驱动描述,王明学learn
usb设备驱动 本章主要内容包含以下:USB总线介绍,USB协议分析,USB系统架构 一.USB总线介绍 1.1USB发展史 USB(Universal Serial Bus)通用串行总线,是一种外部 ...
- winedt打开.tex文件时会出现reading error,看不到任何文字
winedt打开.tex文件时会出现reading error,然后看不到任何文字. 解决办法:先打开空白的winedt,然后点击open,找到该.tex文件,将文件名右侧的 default 改为 ...
- 【项目经验】--EasyUI DataGrid之右键菜单
前两天验收项目,老总提了一个不是需求的需求,为什么这么说呢?因为我们的管理不到位!话说当天,我们UI系统下发了一个总文件,上面写着"各个系统找一个没有添加UI的模块去添加最新版本UI进行测试 ...
- 解决android expandablelistview 里面嵌入gridview行数据重复问题
最近做了一个“csdn专家博客App” 当然了是android版本,在专家浏览页面,我才用了expandablelistview 组件来显示专家分类,每个分类点击之后可以显示专家的头像和名字. 很简单 ...
- loadrunner通过C语言实现自定义字符出现次数截取对应字符串
void lr_custom_string_delim_save(char inputStr[500], char* outputStr, char *delim, int occrNo, int s ...
- loadrunner中lr_save_searched_string函数的使用
控制abc后面的第几个字符: 控制字符长度: 总结: 实际应用: Action() { int i =0,j=0; char *tt_url = lr_eval_string("{tt_ur ...
- ubuntu输入su命令显示 Authentication failure解决
由于ubuntu系统默认是没有激活root用户的,需要手动激活: 终端下输入: sudo passwd Password:你当前的密码 Enter new UNIX password:这个是root的 ...
- 常用meta标签举例说明
本文是作者从百度百科和其他从网页中搜索到的资料,经综合整理,把常用meta标签列举并示例说明,仅供参考. 1.<meta charset="UTF-8"> --- ch ...
- 每天一个linux命令---telnet
执行telnet指令开启终端机阶段作业,并登入远端主机. telnet的命令的格式: telnet ip port 例1: 建立连接不成功 [richmail@portal bin]$ telne ...