标签:strong col 范围 数字 题目 char turn ring describe
按我的看法来说这两个题实在没啥意思,所以写一块了。需要了解一下罗马数字和整数之间的转换规则即可。
leetcode 136:
1 int romanToInt(string s) { 2 std::map<char, int> m = { {‘I‘,1},{‘V‘,5},{‘X‘,10},{‘L‘,50},{‘C‘,100},{‘D‘,500},{‘M‘,1000} }; 3 int n = s.size(); 4 int sum = m[s.back()]; 5 for (int i = n - 2; i >= 0; i--) 6 { 7 if(m[s[i]] < m[s[i+1]]) 8 sum -= m[s[i]]; 9 else 10 sum = sum + m[s[i]]; 11 } 12 return sum; 13 }
leetcode 137:
1 string intToRoman(int num) { 2 string str; 3 vector<int>m={1,4,5,9,10,40,50,90,100,400,500,900,1000}; 4 vector<string>n={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; 5 for(int i=m.size()-1;i>=0;){ 6 if(num>=m[i]){ 7 str+=n[i]; 8 num=num-m[i]; 9 } 10 else{ 11 i--; 12 } 13 } 14 return str; 15 }
leetcode 136:roman-to-integer&&leetcode 137:integer-to-roman
标签:strong col 范围 数字 题目 char turn ring describe
原文地址:https://www.cnblogs.com/qingjiaowoxiaoxioashou/p/13490191.html