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 ...
随机推荐
- Hadoop开发环境简介(转)
1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...
- static_cast, dynamic_cast, const_cast探讨
转自:http://www.cnblogs.com/chio/archive/2007/07/18/822389.html 首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 ...
- 【Maven】Maven下载源码和Javadoc的方法
1:Maven命令下载源码和javadocs 当在IDE中使用Maven时如果想要看引用的jar包中类的源码和javadoc需要通过maven命令下载这些源码,然后再进行引入,通过mvn命令能够容易的 ...
- labview事件结构
等待事件发生,并执行相应条件分支,处理该事件.事件结构 包括一个或多个子程序框图或事件分支,结构处理时间时,仅有一个子程序框图或分支在执行.等待事件通知时,该结构可超时. 连线边框左上角的“超时”接线 ...
- 队列与DelphiXe新语法
好久没写代码了,更久没上博客园的博客了,无聊写几行试一下新语法. 1 unit Main; interface uses Winapi.Windows, Winapi.Messages, System ...
- Tomcat 系统架构与设计模式,第 1 部分: 工作原理(转载)
简介: 这个分为两个部分的系列文章将研究 Apache Tomcat 的系统架构以及其运用的很多经典设计模式.本文是第 1 部分,将主要从 Tomcat 如何分发请求.如何处理多用户同时请求,还有它的 ...
- angular select中ng-options使用
function selectCtrl($scope) { $scope.selected = ''; $scope.model = [{ id: 10001, mainCategory: '男', ...
- 使用sql访问EXECL文件
--使用sql语句打开访问EXECL文件 --SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT'OpenRowset/OpenDat ...
- 从零新建一个winform项目
网站:https://community.devexpress.com/blogs/eaf/archive/2012/10/30/xaf-application-from-scratch.aspx
- MyEclipse使用总结——使用MyEclipse打包带源码的jar包
平时开发中,我们喜欢将一些类打包成jar包,然后在别的项目中继续使用,不过由于看不到jar包里面的类的源码了,所以也就无法调试,要想调试,那么就只能通过关联源代码的形式,这样或多或少也有一些不方便,今 ...