【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 ...
随机推荐
- jupyter notebook + pyspark 环境搭建
安装并启动jupyter 安装 Anaconda 后, 再安装 jupyter pip install jupyter 设置环境 ipython --ipython-dir= # override t ...
- PHP 线程安全与非线程安全版本的区别深入解析
Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍 ...
- getField方法
getField方法是ThinkPHP中用来获取字段值的方法,区别于select和find方法,通常仅用于获取个别字段的值.但是事实上并没有那么简单,该方法的用法总结如下: 获取某个字段值这个是get ...
- page文件
题目:主页面引用 page 文件 ./configs/style.conf ./templates/main.html <body> <{config_load file=" ...
- STM32F103xx bxCAN(Basic Extended CAN) 滤波机制
一.背景 最近一个项目需要使用STM32F103xx实现CAN通信,而CAN总线的消息滤波在各个MCU上有不同机制, 譬如,SJA1000为标识符位屏蔽滤波机制,NXP的LPC17xx系列为标识符列表 ...
- 转:利用node压缩、合并js,css,图片
1.安装nodejs http://nodejs.org/ 2.安装各自的node package js我用的是UglifyJS github地址:https://github.com/mishoo/ ...
- java之BASE64加解密
1.简介 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,采用Base64编码具有不可读性,即所编码的数据不会被人用肉眼所直接看到. 注:位于jdk的java.util包中. 2. ...
- Jrebel是一套开发环境,用来实现热部署
http://truemylife.iteye.com/blog/1140921 背景与愿景:开发环境下,tomcat对热布署的支持还不够全面,致使开发人员浪费大量时间在重起服务上.为了提高开发效率, ...
- Spring mvc 报错:No qualifying bean of type [java.lang.String] found for dependency:
具体错误: No qualifying bean of type [java.lang.String] found for dependency: expected at least 1 bean w ...
- leetcode 236. Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...