Roman to Integer
来源:互联网 发布:知乎 四个意识 编辑:程序博客网 时间:2024/06/01 20:18
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
类似Integer tp Roman
从string的最后一位开始做判断,在判断类似XL这种情况,当遍历到X,只需判断当前的计算结果ans,是否>=L即可,如果成立,说明是XL这种情况,只需-X,否则+x,以此类推。
int romanToInt(string s) { int ans = 0; for(int i = s.size() - 1; i >= 0; i--){ switch(s[i]){ case 'I': ans += (ans >= 5) ? -1 : 1; break; case 'X': ans += 10 * ((ans >= 50) ? -1 : 1); break; case 'V': ans += 5; break; case 'C': ans += 100 * ((ans >= 500) ? -1 : 1); break; case 'L': ans += 50; break; case 'M': ans += 1000; break; case 'D': ans += 500; break; } } return ans; }
0 0
- Integer to Roman & Roman to Integer
- Integer to Roman & Roman to Integer
- 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
- Integer to Roman and Roman to Integer
- LeetCode Integer to Roman & Roman to Integer
- Roman to Integer/Integer to Roman
- leetcode - roman to integer & integer to roman
- "Roman to Integer" and "Integer to Roman"
- Roman to Integer &&Integer to Roman
- Roman to Integer and Integer to Roman
- Roman to Integer/Integer to Roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- 1801: [Ahoi2009]chess 中国象棋
- Matlab教程(一)
- 2014/6/8
- gdb的脚本使用 -- gdbinit
- rfkill的用法
- Roman to Integer
- gdb脚本编写
- XCode gdb/lldb 调试命令
- Python学习笔记二---OS.Path模块
- 详解如何将cocos2dx项目编译到Android平台上的(方式一:Cywin+NDK)
- Xcode 用lldb命令po(print-object)打印UIView的视图层级
- 从零开始教你制作cocos2dx-3.0 版本FlappyBird(可上架版本)(包括添加广告等)完整制作过程
- ural 1297 Palindrome ( 后缀数组 最长回文子串 )
- XTU 1206Dormitory's Elevator(dp)