【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
思路:首先要学一下罗马数字是怎么表示的,参见百度百科

其实看了上面罗马数字的介绍,可以建一个对应表 把数字和字母对应起来。遇到 I X C且后面字母的数字更大时 减去当前数字,否则加上。
int romanToInt(string s) {
int num = ;
int c[];
c['I'] = ; c['V'] = ; c['X'] = ;
c['L'] = ; c['C'] = ; c['D'] = ; c['M'] = ;
for(int i = ; i < s.size(); ++i)
{
if((s[i] == 'I' || s[i] == 'X' || s[i] == 'C') && (i + < s.size() && c[s[i + ]] > c[s[i]])) //如果当前是I X C 且后面的数字更大 减去当前数字
num -= c[s[i]];
else
num += c[s[i]];
}
return num;
}
Integer to Roman
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
思路:还是用到查找表。不过凡是有4或者9的都比较特殊,所以也放在表里,之后查表即可。
string intToRoman(int num) {
vector<pair<int, string>> v;
v.push_back(make_pair(, "M"));
v.push_back(make_pair(, "CM"));
v.push_back(make_pair(, "D"));
v.push_back(make_pair(, "CD"));
v.push_back(make_pair(, "C"));
v.push_back(make_pair(, "XC"));
v.push_back(make_pair(, "L"));
v.push_back(make_pair(, "XL"));
v.push_back(make_pair(, "X"));
v.push_back(make_pair(, "IX"));
v.push_back(make_pair(, "V"));
v.push_back(make_pair(, "IV"));
v.push_back(make_pair(, "I"));
string roman;
for(int i = ; i < v.size(); ++i)
{
int n = num / v[i].first;
if(((i & 0x1) == ) && n > )
{
roman += v[i].second;
num -= v[i].first;
}
else if(((i & 0x1) == ))
{
while(n--)
roman += v[i].second;
num = num % v[i].first;
}
}
return roman;
}
更快的代码,表做的更全,循环都省了:
class Solution {
public:
const static string THOUS[];
const static string HUNDS[];
const static string TENS[];
const static string ONES[];
string intToRoman(int num) {
string result;
result += THOUS[(int)(num/)%];
result += HUNDS[(int)(num/)%];
result += TENS[(int)(num/)%];
result += ONES[num%];
return result;
}
};
const string Solution::THOUS[] = {"","M","MM","MMM"};
const string Solution::HUNDS[] = {"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
const string Solution::TENS[] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
const string Solution::ONES[] = {"","I","II","III","IV","V","VI","VII","VIII","IX"};
【leetcode】Integer to Roman & Roman to Integer(easy)的更多相关文章
- 【LeetCode】518. Coin Change 2 解题报告(Python)
[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ...
- 【LeetCode】785. Is Graph Bipartite? 解题报告(Python)
[LeetCode]785. Is Graph Bipartite? 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu. ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】764. Largest Plus Sign 解题报告(Python)
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
- 【LeetCode】738. Monotone Increasing Digits 解题报告(Python)
[LeetCode]738. Monotone Increasing Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】227. Basic Calculator II 解题报告(Python)
[LeetCode]227. Basic Calculator II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...
- 【LeetCode】640. Solve the Equation 解题报告(Python)
[LeetCode]640. Solve the Equation 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ...
- 【LeetCode】743. Network Delay Time 解题报告(Python)
[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ...
随机推荐
- Todd's Matlab讲义第4讲:控制误差和条件语句
误差和残量 数值求解方程\(f(x)=0\)的根,有多种方法测算结果的近似程度.最直接的方法是计算误差.第\(n\)步迭代结果与真值\(x^\*\)的差即为第\(n\)步迭代的误差: \begin{e ...
- nyoj 252 01串 动态规划( java)
当n=2时, 输出 3:当n=3时, 输出 5:当n=4时, 输出 8: #### 分析: 当n=4时,如 0101 符合条件, 当第一个位置是0时,还剩3个位置 ,与n=3时个数相等: 符合条件的为 ...
- HDOJ 4717 The Moving Points
The Moving Points Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- UESTC 1851 Kings on a Chessboard
状压DP... Kings on a Chessboard Time Limit: 10000ms Memory Limit: 65535KB This problem will be judged ...
- Android连接蓝牙耳机播放音乐
参考: Android实现主动连接蓝牙耳机 具体实现: private static final String TAG = "BluetoothA2DPTest"; private ...
- springmvc之DispatcherServlet
1.作用:DispatcherServlet是前置控制器,配置在web.xml(因为DispatcherServlet是一个servelet)文件中的.拦截匹配的请求,Servlet拦截匹配规则要自已 ...
- 在同一个页面使用多个不同的jQuery版本,让它们并存而不冲突
- jQuery自诞生以来,版本越来越多,而且jQuery官网的新版本还在不断的更新和发布中,现已经达到了1.6.4版本,但是我们在以前的项目中就已经使用了旧版本的jQuery,比如已经出现的:1.3 ...
- oracle 彻底删除用户及表空间
1.删除表空间 可以先将其offline alter tablespace xxx offline; 将磁盘上的数据文件一同删除 drop tablespace xxx including conte ...
- iOS开发——UI基础-UIImage,UIImageView的使用
1.UIImage 创建UIImage的两种方法 UIImage *image = [UIImage imageNamed:imageNmae]; UIImage *image = [UIImage ...
- 两款基于Jquery的图表插件
一.EasyPieChart 页面加载时,运行initPieChart()函数,调用easyPieChart()函数,显示出图表. 代码: var initPieChart = function() ...