Leetcode 13 Roman to Integer 字符串处理+STL
题意:将罗马数字1到3999转化成自然数字,这里用了STL库map将罗马字符映射到自然数字。
I,V,X,L,C,D,M -> 1,5,10,50,100,500,1000
m[s[i]]<m[s[i+1]//如IV 就需要减去1
class Solution {
public:
map<char,int> m;
Solution(){
const int N = ;
char str[N+] = "IVXLCDM";
int num[N] ={,,,,,,};
for (int i = ; i < N; ++i){
m[str[i]] = num[i];
}
}
~Solution(){
m.clear();
}
int romanToInt(string s) {
int ans = ;
for(int i = ;i<s.size()-;++i){
if (m[s[i]]<m[s[i+]]) ans -= m[s[i]];
else ans += m[s[i]];
}
ans += m[s[s.size() - ]];
return ans;
}
};
Leetcode 13 Roman to Integer 字符串处理+STL的更多相关文章
- 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 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- [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 - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [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] 13. Roman to Integer ☆☆
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- 新创建的项目利用git添加远程仓库
git initgit remote add origin https:// //git账号中的网址 (git remote //可以查询添加的远程仓库) git add . //添加刚刚导入的整个工 ...
- 通过字符串寻找与字符串一致的model的属性
// 取得选中权限集合 string[] arrAuthorityId = this.hidAuthorityIds.Value.TrimEnd(',').Split(','); BLBQ_Autho ...
- C#的UDP服务器
最新优化版本 /* http://www.cnblogs.com/zengqinglei/archive/2013/04/27/3046119.html */ using System; using ...
- python 迭代器和生成器
1.迭代器协议是指:对象必须提供一个next方法,执行该方法要么返回迭代中的下一项,要么就引起一个StopIteration异常,以终止迭代 (只能往后走不能往前退)2.可迭代对象:实现了迭代器协议的 ...
- session的使用方法
概念:session把客户资料存在服务器中,给浏览器一个加密凭证,每次登录生成的凭证都不相同,浏览器用cookie保存凭证.下次访问时服务器收到凭证后,打开文件读取session信息.session_ ...
- Frogger
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sit ...
- UE简单操作
一:UE恢复默认配置 1.开始--运行-- “%APPDATA%” 回车 . 2. 找到并打开 IDMComp 文件夹.将文件夹“UltraEdit”整个给删除. 3.重 ...
- Splunk及splunkforward简单部署配置
部署环境 操作系统 服务器操作系统版本:CentOS release 6.5 (Final) 2.6.32-431.el6.x86_64 软件 软件版本:splunk-6.4.0 tar: splun ...
- UVALive 3942 Remember the Word(字典树+DP)
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...
- vijos1144(小胖守皇宫)
也是ural1039 描述 huyichen世子事件后,xuzhenyi成了皇上特聘的御前一品侍卫. 皇宫以午门为起点,直到后宫嫔妃们的寝宫,呈一棵树的形状:某些宫殿间可以互相望见.大内保卫森严,三步 ...