Roman to Integer & Integer to Roman
题目:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解析:
这题没兴趣做,抄答案
http://blog.csdn.net/jellyyin/article/details/13165731
class Solution {
public:
int romanToInt(string s) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int result=;
map<char,int> roman;
roman['I']=;
roman['V']=;
roman['X']=;
roman['L']=;
roman['C']=;
roman['D']=;
roman['M']=;
for(int i=s.length()-;i>=;i--)
{
if(i==s.length()-)
{
result=roman[s[i]];
continue;
}
if(roman[s[i]] >= roman[s[i+]])
result+=roman[s[i]];
else
result-=roman[s[i]];
}
return result;
}
};
题目:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
解析:
class Solution {
public:
string intToRoman(int num) {
const int radix[] = {, , , , , ,
, , , , , , };
const string symbol[] = {"M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I"};
string roman;
for (size_t i = ; num > ; ++i) {
int count = num / radix[i];
num %= radix[i];
for (; count > ; --count) roman += symbol[i];
}
return roman;
}
};
Roman to Integer & Integer to Roman的更多相关文章
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- Roman to Integer && Integer to Roman 解答
Roman Numeral Chart V:5 X:10 L:50 C:100 D:500 M:1000 规则: 1. 重复次数表示该数的倍数2. 右加左减:较大的罗马数字右边记上较小的罗马数字,表示 ...
- [string]Roman to Integer,Integer to Roman
一.Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within ...
- list<Integer>,Integer[],int[]之间的互转(jdk1.8)
偶然在开发过程中需要将int[] 转成 List<Integer>,采用了遍历的方式,写的代码实在太多. List<Integer> list = new ArrayList& ...
- java面试基础题------》int Integer Integer.valueOf
在jdk1.5的环境下,有如下4条语句: 1 2 3 4 Integer i01 = 59; int i02 = 59; Integer i03 =Integer.valueOf(59); Integ ...
- PostgresException: 42883: function ifnull(integer, integer) does not exist
原因在于PostGresql并没有自带IFNULL函数,可以用COALESCE来替代IFNULL,且COALESCE功能更强大,可以输入更多参数,顺序判断并返回第一个非null值. 例如: SELEC ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
随机推荐
- valgrind检查C/C++内存泄漏
valgrind --tool=memcheck --leak-check=full ./httptest valgrind --tool=memcheck --leak-check=full -- ...
- 手脱UPX v0.89.6 - v1.02
声明: 只为纪录自己的脱壳历程,高手勿喷 这个壳的脱法很多一般都一步直达的,步过我喜欢ESP定律 1.载入OD,在入口下一行ESP定律运行一次 > pushad ; //入口 BE mov es ...
- HBase基本操作-Java实现
创建Table public static void createTable(String tableName){ try { HBaseAdmin hbaseAdmin = new HBaseAdm ...
- node.js的安装配置——前端的配置
最近琢磨了以下node.js的安装,npm的配置,使用gulp watch监听index.html文件的修改,利用服务器打开网页. 打开自己写的网页不要本地双击打开,这样打开的网址是file:///E ...
- aidl.exe'' finished with non-zero exit value 1问题解决【转载】
PS:Android Studio用AIDL时,碰到一个非常棘手的问题,但是百度之,压根非法解决,FQ出去,终于找到了一篇解决问题的文章,特地转载之. 之前使用aidl传递的都是基本的数据类型比如in ...
- HDU 2159 FATE (dp)
题目链接 Problem Description 最近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完这最 ...
- python之yagmail库笔记
1. yagmail是啥 yagmail是给正常人用的,封装的比较彻底的一个python邮件库,发送接收邮件只需要几行代码,炒鸡简单. 2. 安装 使用pip安装,炒鸡简单: pip install ...
- Master of Phi (欧拉函数 + 积性函数的性质 + 狄利克雷卷积)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6265 题目大意:首先T是测试组数,n代表当前这个数的因子的种类,然后接下来的p和q,代表当前这个数的因 ...
- es6新语法Object.assign()
1.介绍 Object.assign用于对象的合并,将源对象的所有可枚举属性复制到目标对象,只拷贝源对象自身的属性继承属性补考呗 Object.assign(target,source1,...)第一 ...
- XGBoost与LightGBM对比分析(转)
尊重原创 来源: https://blog.csdn.net/a790209714/article/details/78086867 XGBoost的四大改进: ①改进残差函数 不用Gini作为残 ...