LeetCode13 Roman to Integer
题意:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
分析:
开始准备采用将罗马数字的字符串先划分千位,百位等,但是实际操作起来代码太复杂,而且自己想的逻辑也有问题。
最后采用的方式是从前到后遍历一遍字符即可,加判断来查看字符是不是与前面字符一起组成“小数在大数左边的数字”;
然后依次加数到结果进去即可。
代码:
class Solution {
public:
int romanToInt(string s) {
int result = ;
for (int i = ; i < s.size(); ++i) {
if (s[i] == 'M') {
if (i - >= && s[i-] == 'C') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'D') {
if (i - >= && s[i-] == 'C') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'C') {
if (i - >= && s[i-] == 'X') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'L') {
if (i - >= && s[i-] == 'X') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'X') {
if (i - >= && s[i-] == 'I') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'V') {
if (i - >= && s[i-] == 'I') {
result += ;
}
else {
result += ;
}
}
if (s[i] == 'I'){
result += ;
}
}
return result;
}
};
LeetCode13 Roman to Integer的更多相关文章
- 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)
13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符 数值 I 1 V ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- 5.Integer to Roman && Roman to Integer
Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
随机推荐
- 第二百二十四天 how can I 坚持
实物商品兑换,有点小难搞,其实也没什么难的,也就那些东西,不过好像这就是我设计实现的,干起来挺来劲的. 供暖了,挺暖和的,哈哈. 小米耳机(炫彩版)到了.感觉挺好的. 还在纠结到底要买哪种颜色的羽绒服 ...
- jdk5下载链接
查看jdk版本 java -version JDK下载 最新版本 http://www.oracle.com/technetwork/java/javase/downloads/index.html ...
- 架设证书服务器 及 让IIS启用HTTPS服务
一.架设证书服务器(CA服务)1.在系统控制面板中,找到“添加/删除程序”,点击左侧的“添加/删除windows组件”,在列表中找到“证书服务”,安装之.2.CA类型,这里有四种选择,这里以“独立根C ...
- UVaLive 6602 Counting Lattice Squares (找规律)
题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...
- https,https的本地测试环境搭建,asp.net结合https的代码实现,http网站转换成https网站之后遇到的问题
一:什么是https SSL(Security Socket Layer)全称是加密套接字协议层,它位于HTTP协议层和TCP协议层之间,用于建立用户与服务器之间的加密通信,确保所传递信息的安 ...
- ASP.NET MVC 4 中Jquery上传插件Uploadify简单使用-版本:3.2.1
1.官网下载开发包:http://www.uploadify.com/download/,选择免费的Flash版本: 2.解压后,需要用到以下几个文件: 需要修改uploadify.css中取消上传按 ...
- UI:登录窗的自定义键盘
在创建一个自定义键盘的时候遇到的错误 //双重for循环,对于Button上的数字用二维数组 // NSArray * butArr[4][3] = {@[@"1",@&qu ...
- jQuery Deferred(http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_object.html)
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Ehcache(08)——可阻塞的Cache——BlockingCache
http://haohaoxuexi.iteye.com/blog/2119737 可阻塞的Cache—BlockingCache 在上一节我们提到了显示使用Ehcache锁的问题,其实我们还可以隐式 ...
- js正则表达式之解析——URL的正则表达式
首先,此片文章并不是直接告诉你,url的正则表达式是什么,以及怎么使用这个正则表达式去解析一个URL地址,相信这种问题在网络上已经能找到很多.本文的宗旨在于教你如何理解URL的正则表达式,以达到理解正 ...