LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目:
原题:Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this questio
解析:给出一个罗马数字,要求把其转换为一个整数。输入范围在1到3999内。
罗马数字的规则如下:
罗马数字 I V X L C D M 代表的阿拉伯数字 1 5 10 50 100 500 1000 其中又有:
基本数字 Ⅰ、X 、C 中的任何一个、自身连用构成数目、或者放在大数的右边连用构成数目、都不能超过三个;放在大数的左边只能用一个; 不能把基本数字 V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目;放在大数的右边采用相加的方式构成数目、只能使用一个; I只能用在V和X左边; X只能用在L和C左边; C只能用在D和M左边。举例:
·个位数举例Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9·十位数举例Ⅹ-10、Ⅺ-11、Ⅻ-12、XIII-13、XIV-14、XV-15、XVI-16、XVII-17、XVIII-18、XIX-19、XX-20、XXI-21、XXII-22、XXIX-29、XXX-30、XXXIV-34、XXXV-35、XXXIX-39、XL-40、L-50、LI-51、LV-55、LX-60、LXV-65、LXXX-80、XC-90、XCIII-93、XCV-95、XCVIII-98、XCIX-99·百位数举例C-100、CC-200、CCC-300、CD-400、D-500、DC-600、DCC-700、DCCC-800、CM-900、CMXCIX-999·千位数举例M-1000、MC-1100、MCD-1400、MD-1500、MDC-1600、MDCLXVI-1666、MDCCCLXXXVIII-1888、MDCCCXCIX-1899、MCM-1900、MCMLXXVI-1976、MCMLXXXIV-1984、MCMXC-1990、MM-2000、MMMCMXCIX-3999参考:罗马数字
2.思路
有两种思路,一种是直接if-else的模式(这种模式runtime比第二种好),第二种是switch,第二种的runtime表现并不好。这引发了我对 if - else 和 switch的好奇,还好已经有人总结出了这部分的内容:
由此看来,switch有点以空间换时间的意思,而事实上也的确如此。
1.当分支较多时,当时用switch的效率是很高的。因为switch是随机访问的,就是确定了选择值之后直接跳转到那个特定的分支,但是if。。else是遍历所以得可能值,知道找到符合条件的分支。如此看来,switch的效率确实比ifelse要高的多。
2.由上面的汇编代码可知道,switch...case占用较多的代码空间,因为它要生成跳表,特别是当case常量分布范围很大但实际有效值又比较少的情况,switch...case的空间利用率将变得很低。
3.switch...case只能处理case为常量的情况,对非常量的情况是无能为力的。例如 if (a > 1 && a < 100),是无法使用switch...case来处理的。所以,switch只能是在常量选择分支时比ifelse效率高,但是ifelse能应用于更多的场合,ifelse比较灵活。
3.两种思路的代码
第一种:
class Solution {
public:
int romanToInt(string s) {
int ans=0,m=s.size();
char ch;
for(int i=0;i<m;i++){
if(s[i]=='M') ans+=1000;
else if(s[i]=='D') ans+=500;
else if(s[i]=='C') {
if((s[i+1]=='D'||s[i+1]=='M')&&i+1<m) ans-=100;
else ans+=100;
}
else if(s[i]=='X') {
if((s[i+1]=='L'||s[i+1]=='C')&&i+1<m) ans-=10;
else ans+=10;
}
else if(s[i]=='V') ans+=5;
else if(s[i]=='I') {
if((s[i+1]=='V'||s[i+1]=='X')&&i+1<m) ans--;
else ans++;
}
else if(s[i]=='L') ans+=50;
}
return ans;
}
};
第二种:
class Solution {
public:
int value(char ch)
{
switch (ch) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
}
return 0;
}
int romanToInt(string s)
{
int ans = 0;
char max = 'I';
for (int i = s.size()-1; i >= 0; --i) {
if (value(s[i]) >= value(max)) {
max = s[i];
ans += value(s[i]);
} else {
ans -= value(s[i]);
}
}
return ans;
}
};
LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告的更多相关文章
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [leetcode]13. Roman to Integer罗马数字转整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 13. Roman to Integer ☆☆
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [leetcode] 13. Roman to Integer
如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...
随机推荐
- 手写redis客户端
一.RESP通信协议 Redis Serialization Protocol (Redis序列化协议). 特点:容易实现.解析快.可读性强 以\r\n分割数据. 二.撸代码 package com. ...
- Error creating bean with name 'mapper' defined in class path resource [applicationcontext.xml]: Cannot resolve reference to bean 'factory' while setting bean property 'sqlSessionFactory'; nested excep
Error creating bean with name 'mapper' defined in class path resource [applicationcontext.xml]: Cann ...
- javascript最常用的对象创建方式
//第一种 function Demo(){ var obj=new Object(); obj.name="Yubaba"; obj.age=12; obj.firstF=fun ...
- Vue清除所有JS定时器
Vue清除所有JS定时器 在webpack + vue 的项目中如何在页面跳转的时候清除所有的定时器 JS定时器会有一个返回值(数字),通过这个返回值我们可以找到这个定时器 在vue项目中可以使用路由 ...
- Spark 加载数据库mysql表中数据进行分析
1.工程maven依赖包 <properties> <spark_version>2.3.1</spark_version> <!-- elasticsear ...
- python学习笔记:第16天 面向对象02--对象中的成员
目录 ⼀.类的成员介绍: 二.类的成员-变量 三.类的成员-方法 四.类的成员-属性 五.私有属性 ⼀.类的成员介绍: ⾸先, 什么是类的成员. 很简单. 我么能在类中写什么? 写的内容就是成员. 到 ...
- 推荐 的FPGA设计经验(4) 时钟和寄存器控制架构特性使用
Use Clock and Register-Control Architectural Features FPGAs provide device-wide clocks and register ...
- 20145202马超《网络对抗》Exp9*_* Web安全基础实践
本实践的目标理解常用网络攻击技术的基本原理.Webgoat实践下相关实验. 1.实验后回答问题 (1)SQL注入攻击原理,如何防御 感觉上次做的也是sql注入,就是故意的非法输入(输入的是一些指令)让 ...
- 全国Uber优步司机奖励政策 (1月11日-1月17日)
本周已经公开奖励整的城市有:北 京.成 都.重 庆.上 海.深 圳.长 沙.佛 山.广 州.苏 州.杭 州.南 京.宁 波.青 岛.天 津.西 安.武 汉.厦 门,可按CTRL+F,搜城市名快速查找. ...
- 成都Uber优步司机奖励政策(3月22日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...