首页 > 代码库 > LeetCode Roman to Integer
LeetCode Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题意:讲罗马数字转换成阿拉伯数字思路:了解罗马数字的构造后,从后往前处理就行了
class Solution { public: int romanToInt(string s) { if (s.size() == 0) return 0; map<char, int> mp; mp['I'] = 1; mp['V'] = 5; mp['X'] = 10; mp['L'] = 50; mp['C'] = 100; mp['D'] = 500; mp['M'] = 1000; int len = s.size(); int sum = mp[s[len-1]]; for (int i = len-2; i >= 0; i--) { if (mp[s[i]] < mp[s[i+1]]) sum -= mp[s[i]]; else sum += mp[s[i]]; } return sum; } };
LeetCode Roman to Integer
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。