Roman to Integer - Leetcode
来源:互联网 发布:魅力中国网络电视台 编辑:程序博客网 时间:2024/05/19 00:41
题意:将罗马数字转换成整型数
思路:借鉴上一道将整型数转换成罗马数字的第二种方法,先对照列出“I,V,X,L,C,D,M”的字符数组和值数组。然后遍历给出的罗马数字字符串,将获取的字符对照的数值存到一个临时变量k中。
如果下一个字符的数值等于k,则k+=value[j];
如果下面得到的数值大于该变量k,则k=value[j]-k;
直到下一个字符的数值小于k,则将k加入num中;
class Solution {public: int romanToInt(string s) { int values[7] = {1000, 500, 100, 50, 10, 5, 1 }; char numerals[7] = {'M','D','C','L','X','V','I'}; int num=0,k=0; for(int i=0;i<s.size();i++) { int j=0; for(;j<7;j++) { if(numerals[j]==s[i]) break; } if(k!=0&&k<values[j]) { k=values[j]-k; } else if(k==values[j]) { k+=values[j]; } else if(k>values[j]||k==0) { num+=k; k=values[j]; } } num+=k; return num; }};
0 0
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- LeetCode Integer to Roman & Roman to Integer
- leetcode - roman to integer & integer to roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- LeetCode: Integer to Roman
- LeetCode : Roman to Integer
- [Leetcode] Roman to Integer
- [Leetcode] Integer to Roman
- Leetcode: Integer to Roman
- Leetcode: Roman to Integer
- LeetCode Integer to Roman
- 【leetcode】Roman to Integer
- 遗传算法java代码
- 动态规划之01背包问题(最易理解的讲解)
- JavaScript之 ------ JavaScript与html的结合方式
- Android 音乐播放器 (1)界面篇
- IP地址的分配
- Roman to Integer - Leetcode
- pdb文件
- animate.css 动画制作
- 八大排序,各显神通
- Linux 献给初学者:谈谈如何学习Linux操作系统
- cordova-plugin-local-notifications本地通知推送在cordova 5.0.0以上版本编译报错解决办法
- 实现一个简单的滚动条
- PLSQL跳出嵌套循环的小例子
- DataGridView列长度大于30就以...形式显示