LeetCode刷题实战(13):Roman to Integer
题目描述:
13 |
Roman to Integer |
49.5% | Easy |
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
and M
.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II
in Roman numeral, just two one's added together. Twelve is written as, XII
, which is simply X
+ II
. The number twenty seven is written as XXVII
, which is XX
+ V
+ II
.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII
. Instead, the number four is written as IV
. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX
. There are six instances where subtraction is used:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: "III"
Output: 3
Example 2:
Input: "IV"
Output: 4
Example 3:
Input: "IX"
Output: 9
Example 4:
Input: "LVIII"
Output: 58
Explanation: C = 100, L = 50, XXX = 30 and III = 3.
Example 5:
Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
C语言解法
int romanToInt(char* s) {/**根据题目可知,遇到'I','X','C'罗马字符需要判断后面罗马字符内容*/int len = strlen(s);int sum = 0;int i = 0;char rom = s[0];for(i=0; i<len; i++){switch(*(s+i)){case 'M':sum += 1000;break;case 'D':sum += 500;break;case 'C':(*(s+i+1) == 'D' || *(s+i+1) == 'M')?(sum-=100):(sum+=100);break;case 'L':sum += 50;break;case 'X':(*(s+i+1) == 'L' || *(s+i+1) == 'C')?(sum-=10):(sum+=10);break;case 'V':sum+=5;break;case 'I':(*(s+i+1) == 'I' || *(s+i+1) == 0)?(sum++):(sum--);break;}}return sum;}
运行结果:
Python3解法:
class Solution:def romanToInt(self, s):""":type s: str:rtype: int"""Rom_dic = {'I':1, 'V':5, 'X':10, 'L':50, 'C':100, 'D':500, 'M':1000}sum = Rom_dic[s[len(s)-1]]i = len(s) - 2while i >=0:if(Rom_dic[s[i]]<Rom_dic[s[i+1]]):sum -=Rom_dic[s[i]]else:sum += Rom_dic[s[i]]i = i-1return sum
运行结果:
LeetCode刷题实战(13):Roman to Integer相关推荐
- 【leetcode刷题笔记】Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- LeetCode刷题实战216:组合总和 III
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战417:太平洋大西洋水流问题
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战517:超级洗衣机
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战355:设计推特
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战179:最大数
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战168:Excel表列名称
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战69:x 的平方根
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战535:TinyURL 的加密与解密
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战391:完美矩形
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
最新文章
- 【串讲总结】RNN、LSTM、GRU、ConvLSTM、ConvGRU、ST-LSTM
- android游戏boss坐标,热血江湖手游boss刷新点坐标 boss刷新时间 _图文攻略_高分攻略_百度攻略...
- 行内块元素有哪些标签_html的行内元素与块级元素总结
- 微信小程序(6)模板详解 template
- linux ftp做yum源,在RedHat5下架设yum源服务器(FTP)
- 创建容器时[Warning] IPv4 forwarding is disabled. Networking will not work.
- 子窗体列表在菜单中的实现
- EJB 3.0注入和查找简介
- centos6 yum快速安装mysql_centos6.10 yum安装mysql 5.6-Go语言中文社区
- Oreilly.Python.Cookbook(3rd.Edition.May.2013)pdf
- Android完全关闭(退出)应用程序
- 第003讲 无序列表 有序列表 框架
- 1-关于单片机通信数据传输(中断发送,大小端,IEEE754浮点型格式,共用体,空闲中断,环形队列)...
- 5python 体脂率计算(优化版)
- c语言中各个符号的含义总结,C语言中的符号总结
- 解析 DES 加密算法(C语言):
- (适配方案总结)客户薅公司两台ipad,我还要给做适配?
- NER依存关系模型:原理,建模及代码实现
- iOS开发 学习计划图
- URI API——公交、驾车、步行导航
热门文章
- fir滤波器算法c语言程序,FIR滤波器设计C语言程序
- java file 堵塞_单元测试最终在Java 6中阻塞
- 中文邮件营销html模版,怎么制作邮件营销模板?— —邮件格式
- activemq jdbc mysql_activeMQ JDBC Master Slave
- php进入文件目录,php文件目录操作
- vector利用swap()函数进行内存的释放
- 设计模式C++实现 ——观察者模式
- WinCE驱动开发问题精华集锦
- WinCE系统字体的设置
- java各种的不好_译文《最常见的10种Java异常问题》