LeetCode 13. Roman to Integer
2016-07-24 16:28
411 查看
描述
给出一个罗马数字,让你把它转换成正常的十进制数解决
关键是要清楚罗马数字转换成10进制数的运算法则。当一个数比它右边的数大的时候,就加上这两个数;当一个数比它右边的数小的时候,加上右边的那个数减去当前数的差。class Solution { public: int romanToInt(string s) { unordered_map<char, int> arr; arr['I'] = 1; arr['V'] = 5; arr['X'] = 10; arr['L'] = 50; arr['C'] = 100; arr['D'] = 500; arr['M'] = 1000; int lenth = s.size(); int sum = 0; for (int i = 0; i < lenth; ++i){ if (i != 0){ if (arr[s[i]] > arr[s[i - 1]]){ sum = sum - 2 * arr[s[i - 1]] + arr[s[i]]; } else{ sum += arr[s[i]]; } continue; } sum += arr[s[i]]; } return sum; } };
相关文章推荐
- leetcode 179 Largest Number
- leetcode 24 Swap Nodes in Pairs
- leetcode 2 Add Two Numbers 方法1
- leetcode 2 Add Two Numbers 方法2
- leetcode----Longest Substring Without Repeating Characters
- [LeetCode]47 Permutations II
- [LeetCode]65 Valid Number
- [LeetCode]123 Best Time to Buy and Sell Stock III
- [LeetCode] String Reorder Distance Apart
- [LeetCode] Sliding Window Maximum
- [LeetCode] Find the k-th Smallest Element in the Union of Two Sorted Arrays
- [LeetCode] Determine If Two Rectangles Overlap
- [LeetCode] A Distance Maximizing Problem
- leetcode_linearList
- leetcode_linearList02
- 021-Merge Two Sorted Lists(合并两个排好序的单链表);leetcode
- LeetCode[Day 1] Two Sum 题解
- LeetCode[Day 2] Median of Two Sorted Arrays 题解
- LeetCode[Day 3] Longest Substring Without... 题解
- LeetCode [Day 4] Add Two Numbers 题解