LeetCode OJ 13. Roman to Integer
来源:互联网 发布:爱福窝设计软件下载 编辑:程序博客网 时间:2024/05/29 13:25
LeetCode OJ 13. Roman to Integer
Description
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解题思路
罗马数字转换为整数:我们把1,10,100,1000,5,500,500和对应的罗马数字做映射。罗马数字的特点是:如果后一个的字母表示的数字比它自身表示的数字大,那说明这两个数字之间是做减法的,得到的结果加上当且结果;否则直接加上当前结果。以这样的方式遍历完这个罗马数字的字符串,即可得到最终结果。
代码
个人github代码链接
class Solution {public: int romanToInt(string s) { int roman[256] = {0}; int num = 0; roman['I'] = 1;roman['X'] = 10;roman['C'] = 100;roman['M'] = 1000;roman['V'] = 5; roman['L'] = 50;roman['D'] = 500; int i = 0; while(i < s.length()){ if(roman[s[i]] < roman[s[i+1]]){ num -= roman[s[i]]; } else num += roman[s[i]]; i ++; } return num; }};
阅读全文
0 0
- LeetCode OJ 13. Roman to Integer
- LeetCode OJ:Roman to Integer
- LeetCode OJ:Integer to Roman
- LeetCode OJ - Roman to Integer
- LeetCode OJ - Integer to Roman
- [LeetCode OJ]Roman to Integer
- [LeetCode OJ]Integer to Roman
- LeetCode OJ : Roman to Integer
- LeetCode OJ Roman to Integer
- LeetCode OJ Integer to Roman
- LeetCode OJ - Integer to Roman
- LeetCode OJ - Roman to Integer
- <LeetCode OJ> 13 / 12 Roman to Integer & Integer to Roman
- LeetCode OJ 12 Integer to Roman
- LeetCode OJ 13 Roman To Integer
- LeetCode OJ: 12 Integer to Roman
- LeetCode OJ-13-Roman to Integer
- 四、[LeetCode OJ]Integer to Roman
- RabbitMQ (三) 发布/订阅
- Docker容器:网路模式的原理
- 书签
- 基于LSTM搭建一个文本情感分类的深度学习模型:准确率往往有95%以上
- 修复 EasyUI的 linkbutton 的启用和禁用 问题
- LeetCode OJ 13. Roman to Integer
- Python正则表达式re模块简明笔记
- c# 时间格式的转换问题
- jdbc连接oracle负载均衡
- 乌班图lnmp搭建
- PHP底层的运行机制与原理
- 【计算机视觉】EmguCV学习笔记(3)ROI区域图像叠加以及初级图像混合
- iOS 上架报错:PLA 1.2 The seller and company names associated with your app do not reflect the name “XX"
- 1