【LeetCode】Roman to Integer
来源:互联网 发布:阿里云服务器如何续费 编辑:程序博客网 时间:2024/06/03 20:29
题目描述:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路是对字符串从右往左进行遍历。若s[i]>=s[i-1],则加上该字符代表的数字,else则减去该数字。代码如下:
class Solution {public:int romanToInt(string s) {unordered_map<char, int> roman;vector<char> chars = { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };vector<int> nums = { 1, 5, 10, 50, 100, 500, 1000 };for (int i = 0; i < chars.size(); i++)roman[chars[i]] = nums[i];int prev(0), res(0);for (int i = s.length(); i >= 0; i--){char c = s[i];if (roman[c] >= prev)res += roman[c];elseres -= roman[c];prev = roman[c];}return res;}};
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
- Excel使用技巧
- centos 6.4 性能调优之增加虚拟内存
- LDD命令的原理与使用方法
- Hbase shell详情
- 在linux下编译C和C++
- 【LeetCode】Roman to Integer
- Android设计模式—策略模式
- c++的经验!
- RTP/RTCP的时间同步机制
- GDB调试中查看栈信息
- ID3DXFont显示乱码的原因及解决方法
- 字符集编码
- 浅谈ThreadLocal
- 签约黄河的进来,说说去年的体会