Roman to Integer

3999范围内的罗马数字不会用到加上划线的字母

class Solution {
public:
int romanToInt(string s) {
int map[26];
map['I'-'A'] = 1; map['V'-'A'] = 5; map['X'-'A'] = 10; map['L'-'A'] = 50;
map['C'-'A'] = 100; map['D'-'A'] = 500; map['M'-'A'] = 1000;
int res = 0, n = s.size();
s.push_back(s[n-1]);
for(int i = 0; i < n; i++)
{
if(map[s[i]-'A'] >= map[s[i+1]-'A'])
res += map[s[i]-'A'];
else res -= map[s[i]-'A'];
}
return res;
}
};

class Solution {
public:
int romanToInt(string s) {
int map[26];
map['I'-'A'] = 1; map['V'-'A'] = 5; map['X'-'A'] = 10; map['L'-'A'] = 50;
map['C'-'A'] = 100; map['D'-'A'] = 500; map['M'-'A'] = 1000;
int res = 0, n = s.size();
s.push_back(s[n-1]);
for(int i = 0; i < n; i++)
{
if(map[s[i]-'A'] >= map[s[i+1]-'A'])
res += map[s[i]-'A'];
else res -= map[s[i]-'A'];
}
return res;
}
};

Integer to Roman

class Solution {
public:
string intToRoman(int num) {
string roman[4][10] = {
{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"},
{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"},
{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"},
{"", "M", "MM", "MMM"}
};

string ret = "";
int digit = 0;
while (num != 0) {
int remain = num % 10;
ret = roman[digit][remain] + ret;
digit++;
num /= 10;
}

return ret;
}
};

，理解了规则之后就可以这要写了。

class Solution {
public:
string intToRoman(int num) {
string str;
string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[]=    {1000,900,500,400, 100, 90,  50, 40,  10, 9,   5,  4,   1};
for(int i=0;num!=0;++i)
{
while(num>=value[i])
{
num-=value[i];
str+=symbol[i];
}
}
return str;
}
};

[leetcode]Roman to Integer C语言

【题目】 Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be w...
• noc_lemontree
• 2015年08月20日 09:49
• 621

Integer to Roman （罗马数字转换） 【leetcode】

• zhang9801050
• 2013年09月20日 16:09
• 16612

LeetCode13——Roman to Integer

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
• booirror
• 2015年01月27日 17:51
• 2452

【LeetCode】Integer to Roman 和 Roman to Integer 解题报告

【题目】 Given a roman numeral, convert it to an integer. Or, Given an integer, convert it to a roman nu...
• ljiabin
• 2014年10月10日 20:28
• 21617

LeetCode --- 12. Integer to Roman

• makuiyu
• 2015年01月29日 10:49
• 1949

[LeetCode]Roman to Integer,解题报告

• zinss26914
• 2013年12月01日 20:46
• 19458

LeetCode 12 Integer to Roman(C,C++,Java,Python)

Problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the ra...
• runningtortoises
• 2015年05月08日 13:13
• 1104

[LeetCode] 012. Integer to Roman (Medium) (C++/Java/Python)

[LeetCode] 012. Integer to Roman (Medium) (C++/Java/Python)
• hcbbt
• 2015年03月02日 23:08
• 2301

【LeetCode013算法/编程练习C++】Roman to Integer //难得击败了80%的人

• zmdsjtu
• 2016年12月29日 19:37
• 639

（Java）LeetCode-12. Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
• u012848330
• 2016年05月10日 23:13
• 440

举报原因： 您举报文章：LeetCode之Roman & Integer 的转换（简单题） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)