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 ...
随机推荐
- MVC学习地址
http://www.cnblogs.com/n-pei/tag/Asp.net%20MVC/
- C# 代码页获取input的值
<input id="aa" name="iaa" type="text" /> Label1.Text = Request.F ...
- Python笔记——break的注意事项
在python中有个控制流的语句:break 它是用来终止循环语句的,不管此时循环体进行到哪,只要碰到break都停止执行循环语句. 1.举例脚本: #!/usr/bin/env python for ...
- andorid service 本地服务
ActivityManifect.xml <?xml version="1.0" encoding="utf-8"?> <manifest x ...
- jquery中html()、text()、val()的区别与使用
.html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. .html(),.text(),.val()三种方法都 ...
- c# 通过关键字查询
1:首先需要在前端显示界面View视图中添加查询按钮: <div> <div>@Html.NopLabelFor(model => model.IndividualNam ...
- JS实现隔行变色,鼠标移入高亮
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 31.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...
- 修改Broforce无限人数,死亡不减反加
看B站直播发现这个有趣的游戏,找了半天修改器无效,Cheat Engine怎么找指针有点忘了,直接找数值每关都要重来,想来想去还是简单粗暴的反编译好了. 顺便做下C#反编译备忘. 首先把DLL反成IL ...
- git以及git flow 的使用
转载:http://selfcontroller.iteye.com/blog/996494 在这里主要讲一下我在项目中用到的关于gitflow的用法. 公司的项目中,专门有一台用来存放版本库的服 ...
- C# async await 学习笔记1
由于我的开发工具为vs.net 2010(.net 4.0),需先做以下两步才能进行: 1.下载并安装Async CTP (http://www.microsoft.com/en-us/downloa ...