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 ...
随机推荐
- SFM学习
摘自李翠http://www.cnblogs.com/serser/p/6598621.html SFM 1.相机模型,内参数和外参数矩阵,相机标定: 2.极线约束和本征矩阵:特征点提取与匹配:提取到 ...
- HDU1536:S-Nim(sg函数)
S-Nim Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submi ...
- array_diff使用注意
$lost_ids = array_diff($all_ids,$old_ids); //array_diff,$old_ids不可以为null否则返回为null;array_diff起不到效果~~~
- 在Unity中实现屏幕空间反射Screen Space Reflection(3)
本篇讲一下相交检测的优化.有两个措施. 线段相交检测 之前的检测都是检测光线的终点是否在物体内.我们可以尝试检测光线的线段是否与物体相交. 比如说有一个非常薄的物体,光线差不多垂直于它的表面.如果用普 ...
- 3、CSS基本介绍
1.1 CSS基本介绍一.web 标准所谓的web标准指的就是一系列规范网页书写的要求,它是由W3C组织制定,在它里面要求网页的结构.样式.行为三者相分离.二.名词解释1.结构:就是通过HTML标签搭 ...
- Docker微容器Alpine Linux
Alpine 操作系统是一个面向安全的轻型 Linux 发行版. 它不同于通常 Linux 发行版,Alpine 采用了 musl libc 和 busybox 以减小系统的体积和运行时资源消耗,但功 ...
- iOS中UITabelView
1.概述 继承自UIScrollView,只能显示一列数据,只能纵向滑动.堪称UIKit里面最复杂的一个控件了,使用起来不算难,但是要用好并不容易.当使用的时候我们必须要考虑到后台数据的设计,tabl ...
- AUC画图与计算
利用sklearn画AUC曲线 from sklearn.metrics import roc_curve labels=[1,1,0,0,1] preds=[0.8,0.7,0.3,0.6,0.5] ...
- 【IDEA】IDEA中maven项目pom.xml依赖不生效解决
问题: 今天在web项目中需要引入poi相关jar包.查看之下才发现pom.xml中的依赖虽然已经下载到了本地仓库 repository,但是却没有加入到项目路径的 Extenal Libraries ...
- Mysql储存过程5: while
循环结构 while create procedure name() begin while 条件 do SQL语句 end while; end$ create procedure aa6() be ...