【LeetCode】13. Roman to Integer (2 solutions)
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)的更多相关文章
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【leetcode】13. Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. 解题分析: 这道题只要百度一下转换的规则,然后着这解释写代码即可.实现上并没有什么难度,直 ...
- 【一天一道LeetCode】#13. Roman to Integer
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
- 【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 ...
- 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 ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【leetcode❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- LeetCode题解(13)--Roman to Integer
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
随机推荐
- 用java在客户端读取mongodb中的数据并发送至服务器
使用Java自带的socket端口来实现,程序如下: Client.java package com.cn.gao; import java.net.*; import java.io.*; impo ...
- LinkedHashMap源码剖析
首先还是类似的,我们写一个简单的LinkedHashMap的程序: public class Test { public static void main(String[] args) { Map&l ...
- C++中对Mysql的操作函数可以参考以下blog中的内容
http://www.cnblogs.com/lovebread/archive/2009/11/24/1609936.html
- asp.net中使用ueditor 1.3.6上传图片问题
在asp.net中使用ueditor 1.3.6版本上传图片时,出现上传成功但是,图片无法正常显示的问题,解决方法如下: 只需要将imageUp.ashx中的info = up.upFile(cont ...
- ElementUI的提示框的使用记录
1.popover点击之后隐藏 问题描述:做了一个通知面板功能,下面提示信息有路由,每次点击消息呢,就跳转到了路由页面,但是此时这个面板没关闭,希望将其关闭 解决:官方文档有个属性 <div&g ...
- Dijkstra(迪杰斯特拉)算法求解最短路径
过程 首先需要记录每个点到原点的距离,这个距离会在每一轮遍历的过程中刷新.每一个节点到原点的最短路径是其上一个节点(前驱节点)到原点的最短路径加上前驱节点到该节点的距离.以这个原则,经过N轮计算就能得 ...
- linux系统下调度数据库类型资源库中的kettle job
已经存在kettle的一个资源库enfo,在目录/works/wxj下面有一个job (testmailsuccess.kjb)如何实现手工在kettle外部执行此job和让系统每天定时的调用此job ...
- 【C#】C#委托学习
虽然做.NET这行也快2年了,但基础不太好啊,今天看了下委托方面的知识,记录下. 1.委托 总的来说,委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋 ...
- /etc/rc.d/init.d/functions文件详细分析
/etc/rc.d/init.d/functions文件详细分析 functions这个脚本是给/etc/init.d里边的文件使用的(可理解为全局文件). 提供了一些基础的功能,看看里边究竟有些什么 ...
- 使用socket BPF/Linux内核工程导论——网络:Filter(LSF、BPF、eBPF)
使用socket BPF linux 下的 包过滤器 BPF Linux内核工程导论——网络:Filter(LSF.BPF.eBPF) 注意(文中描述的内容): 此外,这段BPF代码还存在的一个问题是 ...