【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 ...
随机推荐
- Google Ads原理
Google AdSense广告会根据访问者的地理IP,显示不同的广告,因为adwords里面有相应的设置,有些广告商只想把广告显示给某一地理区域的客户.另一方面就是同一个网站,你在日本和美国会看到日 ...
- 【C++ Primer】用于大型程序的工具
1. 异常处理 异常以类似于将实參传递给函数的方式抛出和捕获.异常可以是可传给非引用实參的随意实參的类型,这意味着必须可以复制该类型的对象. 当抛出一个表达式的时候,被抛出对象的静态编译时类型将决定异 ...
- 第十四章 Executors源码解析
前边两章介绍了基础线程池ThreadPoolExecutor的使用方式.工作机理.参数详细介绍以及核心源码解析. 具体的介绍请参照: 第十二章 ThreadPoolExecutor使用与工作机理 第十 ...
- [置顶] 一道经典的sql面试题不同的写法
用一条SQL语句 查询出每门课都大于80分的学生姓名,表( #test)如下: Name Course Mark 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 ...
- go语言基础之copy的使用
1.copy的使用 示例: package main //必须有个main包 import "fmt" func main() { srcSlice := []int{1, 2} ...
- HTML5 Canvas,WebGL,CSS Shaders,GLSL的暧昧关系 【转】
HTML5 Canvas,WebGL,CSS Shaders,GLSL的暧昧关系 这篇文章发布于 2011年10月10日,星期一,17:14,归类于 canvas相关. 阅读 58013 次, 今日 ...
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题意: ...
- Jmeter测试报告可视化(Excel, html以及jenkins集成)
做性能测试通常在none GUI的命令行模式下运行Jmeter. 例如: jmeter -n -t /opt/las/JMeter/TestPlan/test.jmx -l /opt/las/JMet ...
- IMA文件如何打开,winimage使用方
一般先用UltraISO打开一个系统的镜像文件(.iso).其中有些文件(尤其是.ima,img)比如下面雨林木风Ghost系统盘的这个IMA文件,我们先提取到桌面 用WinImage打开这个文件即可 ...
- 不兼容:不支持SCSI硬盘
获取机器硬件失败,可能你使用了SCSI硬盘,请更换一台主机进行安装 聚生网管2.11版本不支持scsi硬盘. 终于体会到了不兼容的麻烦了.