Roman to Integer - LeetCode
目录
- 题目链接
- 注意点
- 解法
- 小结
题目链接
Roman to Integer - LeetCode
注意点
- 大的数字在小的数字左边是正常情况,要考虑的是小的数字在大的数字右边的那8种情况
解法
解法一:从右至左,对于出现的每个字符逐个判断,累加。时间复杂度为O(n)
class Solution {
public:int romanToInt(string s) {int i,n = s.size(),anw = 0;for(i = n-1;i >= 0;i--){if(s[i] == 'M'){if(s[i-1] == 'C'){anw += 900;i--;}else{anw += 1000;}}else if(s[i] == 'D'){if(s[i-1] == 'C'){anw += 400;i--;}else{anw += 500;}}else if(s[i] == 'C'){if(s[i-1] == 'X'){anw += 90;i--;}else{anw += 100;}}else if(s[i] == 'L'){if(s[i-1] == 'X'){anw += 40;i--;}else{anw += 50;}}else if(s[i] == 'X'){if(s[i-1] == 'I'){anw += 9;i--;}else{anw += 10;}}else if(s[i] == 'V'){if(s[i-1] == 'I'){anw += 4;i--;}else{anw += 5;}}else if(s[i] == 'I'){anw += 1;}//printf("%d\n",anw);}return anw;}
};
解法二:从左至右。如果之前的数字小于当前的就减去之前的数字,否则就加上。最后一个字符无论如何都加上。时间复杂度O(n)
class Solution {
public:int romanToInt(string s) {map<char,int> m;m['I'] = 1;m['V'] = 5;m['X'] = 10;m['L'] = 50;m['C'] = 100;m['D'] = 500;m['M'] = 1000;int i,n = s.size(),anw = 0;int before = m[s[0]],now = 0;for(i = 1;i < n;i++){now = m[s[i]];if(before < now){anw -= before;}else{anw += before;}before = now;}anw += before;return anw;}
};
小结
- 理解了罗马数字的原理这题就不难
转载于:https://www.cnblogs.com/multhree/p/10320230.html
Roman to Integer - LeetCode相关推荐
- Roman to Integer LeetCode Java
描述 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- # LeetCode集锦(四) - 第13题 Roman To Integer
LeetCode集锦(四) - 第13题 Roman To Integer 问题 Roman numerals are represented by seven different symbols: ...
- LeetCode 13. Roman to Integer
问题链接 LeetCode 13. Roman to Integer 题目解析 将罗马数字转换成普通数字. 解题思路 先简单了解一下什么是罗马数字. 基本字符:I,V,X,L,C,D,M 相应的阿拉伯 ...
- [LeetCode][JavaScript]Roman to Integer
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode算法入门- Roman to Integer Integer to Roman -day8
LeetCode算法入门- Roman to Integer -day8 Roman to Integer: 题目描述: Roman numerals are represented by seven ...
- leetcode 8 Roman to Integer C++实现
leetcode 8 Roman to Integer C++实现 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D ...
- leetcode 12 ,13 Integer to Roman amp;amp;Roman to Integer 罗马与阿拉伯数组转换
12 Integer to Roman 13 Roman to Integer 有可能不注意的结果: class Solution {public:/*1.相同的数字连写,所表示的数等于这些数字相加得 ...
- 罗马数字 java_【leetcode刷题】[简单]13.罗马数字转整数(roman to integer)-java
罗马数字转整数 roman to integer 题目 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M ...
- LeetCode刷题实战(13):Roman to Integer
题目描述: 13 Roman to Integer 49.5% Easy Roman numerals are represented by seven different symbols: I, ...
最新文章
- android百度地图轨迹实现,android 获取GPS经纬度在百度地图上绘制轨迹
- Android 使用摄像头拍照
- 双十一:你收到了多少商家短信
- 如何让U盘支持大于4G的文件
- Win8 .NET Framework 3.5 离线安装
- unet作为服务器不显示画面,UNet:无法生成服务器对象(不使用NetworkManager)
- 做一个“多人在线编辑器”,你会怎么开始
- 中国IT业发展案例剖析之--豪杰解霸
- 正则表达式 数字和小数点_《自然语言处理综论》第三版笔记(二)之正则表达式,文本标准化和编辑距离...
- 周志华机器学习西瓜书速记第二章绪论模型评估与选择(二)
- 论文笔记:3DMM(ACM1999)
- 【数据库】E-R图相关知识、绘制方法及工具推荐
- java retainall源码,Java CopyOnWriteArraySet retainAll()用法及代码示例
- 海康摄像头配置、国标平台接入、萤石云平台、局域网拉流方法
- Base-calling for next-generation sequencing platforms (译文)
- w ndows英文读音,英语名词变复数的发音规则
- ES新建模板时的错误 Client error: `PUT http://127.0.0.1:9200/_template/tmp` resulted in a `400 Bad Request`
- 任你和QQ陌生人聊天
- 使用Python+OpenCV+Tensorflow+Flask实现检测X光图像中的COVID-19(新冠病毒)
- Nginx反向代理Gοοgle
热门文章
- 【收藏】Sentinel 高可用流量管理框架、服务熔断降级等
- golang基本数据类型string字符串常用方法代码示例
- K8S集群的搭建:环境准备及相关命令
- 请你解释一下什么是线程池(thread pool)?
- MySQL Join Buffer参数查询命令
- QML和C ++之间的数据类型转换---枚举
- Qt for Android调用android原生控件安装apk
- 计算机考研四大金刚,“四大金刚”考研一个都不少
- 5.1.1 什么是I/O设备?有几类I/O设备?
- html中内联的form,bootstrap3.0教程之表单(form)使用详解