题意:

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的更多相关文章

  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 ...

  2. LeetCode 13. 罗马数字转整数(Roman to Integer)

    13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符        数值  I           1  V  ...

  3. 【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 ...

  4. 【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 ...

  5. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  6. 5.Integer to Roman && Roman to Integer

    Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...

  7. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  8. 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]

    [本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...

  9. No.013 Roman to Integer

    13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...

随机推荐

  1. 【转】17种常用的JS正则表达式 非负浮点数 非负正数.

    <input type='text' id='SYS_PAGE_JumpPage' name='SYS_PAGE_JumpPage' size='3' maxlength='5' onkeyup ...

  2. Matlab 图像画在坐标轴下

    >> x=linspace(,*pi,); >> y=sin(x); >> figure;plot(x,y,'r-') >> set(gca,'xaxi ...

  3. 开着奥迪做Uber司机是什么心态?

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  4. Servlet 2.4 规范之第一篇:概览

          写在前面的话: 本系列是对<Java Servlet Specification Version 2.4>的完全翻译,力争但不保证完美表达出英文原文的思想内涵.如有疏漏之处,还 ...

  5. hdoj 5328 Problems killer

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5328 #include<stdio.h> #include<algorithm> ...

  6. 如何用C++语言编程(How to program in C++)

    这几年在公司一直带徒弟,每次必教的内容就是C++.在我看来,C++已经有非常好的教材了(注1),实在没有必要从头教起.自学就可以了,可是结果总是不尽人意. 不想再重复一次"把C++当成一门新 ...

  7. Junit4单元测试

    在Eclipse中使用JUnit4进行单元测试(初级篇) 在Eclipse中使用JUnit4进行单元测试(中级篇) 在Eclipse中使用JUnit4进行单元测试(高级篇)

  8. MFC 构建、消亡 顺序 (一)--单文档 (SDI)

    MFC 构建.消亡 顺序 (一)--单文档 (SDI) by:http://www.cnblogs.com/vranger/ (一)SDI 生成顺序 (二)打开文档-“Open” (三)新建文档-“N ...

  9. 跟SAP系统集成的Android应用

    首先吐槽一点,这是我的第一个Android应用,很糙. 这个应用适合于上了SAP系统的企业内部使用,并且限于制造型MTO模式,需要针对生产订单报工操作的场景,因为此应用主要的一个目的,就是用来方便报工 ...

  10. java懒汉式单例遇到多线程

    单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 在计算机系统中,线程池.缓存.日志对象.对话框.打印机.显卡的驱动程序对象常被设计成单例.这些应用都或多或少具有资源管理器的功 ...