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 n = ;
char lastC = ;
for(int i = ; i < s.size(); i ++)
{
switch(s[i])
{
case 'I':
n += ;
lastC = s[i];
break;
case 'V':
if(lastC == 'I')
{//IV
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'X':
if(lastC == 'I')
{//IX
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'L':
if(lastC == 'X')
{//XL
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'C':
if(lastC == 'X')
{//XC
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'D':
if(lastC == 'C')
{//CD
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'M':
if(lastC == 'C')
{//CM
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
default:
return ;
}
}
return n;
}
};

解法二:递归

处理开头字符情况之后递归处理后续字符串

class Solution {
public:
int romanToInt(string s) {
if(s == "")
return ;
//to here, s.size() >= 1
else if(s[] == 'M')
return + romanToInt(s.substr());
else if(s[] == 'C')
{
if(s.size() == )
//s == "C"
return ;
if(s[] == 'M')
return + romanToInt(s.substr());
else if(s[] == 'D')
return + romanToInt(s.substr());
else
return + romanToInt(s.substr());
}
else if(s[] == 'D')
return + romanToInt(s.substr());
else if(s[] == 'X')
{
if(s.size() == )
//s == "X"
return ;
if(s[] == 'C')
return + romanToInt(s.substr());
else if(s[] == 'L')
return + romanToInt(s.substr());
else
return + romanToInt(s.substr());
}
else if(s[] == 'L')
return + romanToInt(s.substr());
else if(s[] == 'I')
{
if(s.size() == )
//s == "I"
return ;
if(s[] == 'X')
return + romanToInt(s.substr());
else if(s[] == 'V')
return + romanToInt(s.substr());
else
return + romanToInt(s.substr());
}
else if(s[] == 'V')
return + romanToInt(s.substr());
}
};

【LeetCode】13. Roman to Integer (2 solutions)的更多相关文章

  1. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  2. 【leetcode】13. Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...

  3. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  4. 【LeetCode】013. Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  5. C# 写 LeetCode easy #13 Roman to Integer

    13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and  ...

  6. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. 【leetcode❤python】13. Roman to Integer

    #-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...

  8. LeetCode题解(13)--Roman to Integer

    https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...

  9. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

随机推荐

  1. Informatica 常用组件Source Qualifier之八 会话前和会话后 SQL

      可以在源限定符转换的"属性"选项卡中添加会话前和会话后 SQL 命令.您可能要使用会话前 SQL 以在会话开始时将时间标识行写入源表. PowerCenter 在读取源之前对源 ...

  2. fpga状态机详解

    什么是状态机:状态机通过不同的状态迁移来完成特定的逻辑操作 状态机的分类:Moore型状态机和Mealy型状态机 Moore型:状态机的变化只与当前的状态有关 Mealy型:状态机的变化不仅与当前的状 ...

  3. Largest Rectangle in Histogram leetcode java

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  4. jquery圆角插件

    为了实现div的圆角效果,你还在用古老的背景图片拼凑的方法吗?还是在用各种浏览器不互相兼容的CSS方式?如果你还在用这样的方式实现圆角,那我告诉你你真的out了,或许是我out了,竟然以前没发现有这样 ...

  5. Android之属性动画(二)

    上一篇文章(链接:http://www.cnblogs.com/jerehedu/p/4458928.html  ),我们对属性动画有了简单的认识,并实际动手使用ObjectAnimator.Anim ...

  6. 关于PHP写的投票网站之刷票风云

    最近学校导航站找我让我给他们做一个投票系统的网站,我一口答应了,他们只是要求不准刷票情况出现,我也一口答应了..我答应的太干脆了. 然后我便开始做这个网站,网站做出来没花太多时间,并且我是用IP来判断 ...

  7. ubuntu添加默认路由才可以访问网络

  8. loadicon后一定要调用destroyicon吗

    Remarks It is only necessary to call DestroyIcon for icons and cursors created with the following fu ...

  9. cocos2d-x -3.81+win7+vs2013开发环境创建新的项目

    cocos2d-x -3.81+win7+vs2013开发环境创建新的项目 1.准备阶段 (1) vs2013下载及安装 (2)cocos2d-x 3.8.1下载及解压 (3)python下载及安装( ...

  10. wireshark 的使用(filter的用法)

    转自:http://blog.csdn.net/hanyuxinting/article/details/5558095 过滤器语法---------------------------------- ...