LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科
罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。按照下述的规则可以表示任意正整数。需要注意的是罗马数字中没有“0”,与进位制无关。一般认为罗马数字只用来记数,而不作演算。
- 重复数次:一个罗马数字重复几次,就表示这个数的几倍。
- 右加左减:
- 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
- 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。
- 左减的数字有限制,仅限于I、X、C。比如45不可以写成VL,只能是XLV
- 但是,左减时不可跨越一个位数。比如,99不可以用IC(
)表示,而是用XCIX(
)表示。(等同于阿拉伯数字每位数字分别表示。) - 左减数字必须为一位,比如8写成VIII,而非IIX。
- 右加数字不可连续超过三位,比如14写成XIV,而非XIIII。(见下方“数码限制”一项。)
- 加线乘千:
- 在罗马数字的上方加上一条横线或者加上下标的Ⅿ,表示将这个数乘以1000,即是原数的1000倍。
- 同理,如果上方有两条横线,即是原数的1000000(
)倍。
Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
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;
}
};
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999
我们注意到罗马数字的字母是有规律的,可以分成几组,I(1), V(5) 是一组, X(10), L(50)是一组, C(100), D(500)是一组, M(1000), d(应该是D加一个上划线,表示5000) 是一组 ……。后一组的两个数是前一组的10倍。
对于大于10的整数,我们把该整数逐位表示成罗马数字。 本文地址
个位上的数字1~9的分别为: I II III IV V VI VII VIII IX
十位上的数字1~9,只要把原来个位上的I 替换成 X, V 替换成L,X替换成C,即十位上的1~9表示的是10~90.
百位、千位以此类推。。。。。。
class Solution {
public:
string intToRoman(int num) {
char romanChar[] = {'I','V','X','L','C','D','M'};
string res;
int i = 6, factor = 1000;
while(num != 0)
{
helper(num / factor, &romanChar[i], res);
i -= 2;
num %= factor;
factor /= 10;
}
return res;
}
void helper(int k, char romanChar[], string &res)
{// 0 <= k <= 9
if(k <= 0);
else if(k <= 3)
res.append(k, romanChar[0]);
else if(k == 4)
{
res.push_back(romanChar[0]);
res.push_back(romanChar[1]);
}
else if(k <= 8)
{
res.push_back(romanChar[1]);
res.append(k-5, romanChar[0]);
}
else if(k == 9)
{
res.push_back(romanChar[0]);
res.push_back(romanChar[2]);
}
}
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3793503.html
LeetCode: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】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- leetcode之旅(10)-Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- Leetcode 题目整理-3 Palindrome Number & Roman to Integer
9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...
- 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 ...
- Roman to Integer & Integer to Roman
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 乘风破浪:LeetCode真题_007_Reverse Integer
乘风破浪:LeetCode真题_007_Reverse Integer 一.前言 这是一个比较简单的问题了,将整数翻转,主要考察了取整和取余,以及灵活地使用long型变量防止越界的问题. 二.Reve ...
- Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串)
Leetcode 8. String to Integer (atoi) atoi函数实现 (字符串) 题目描述 实现atoi函数,将一个字符串转化为数字 测试样例 Input: "42&q ...
随机推荐
- 20145225《Java程序设计》 第4周学习总结
20145225<Java程序设计> 第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1继承 继承共同行为:存在着重复,可把相同的程序代码提升(pull up)为父类.exten ...
- TYVJ计算几何
今天讲了计算几何,发几道水水的tyvj上的题解... 计算几何好难啊!@Mrs.General....怎么办.... 这几道题都是在省选之前做的,所以前面的Point运算啊,dcmp啊,什么什么的,基 ...
- java 类的关系
在面向对象中,类与类之间的关系有泛化,依赖,关联,聚合,组合几种.其中,聚合和组合都属于关联.在具体编程中: 依赖表现为如果A类依赖于B,则B体现为A的局部变量,方法参数或静态方法的调用.eg:cla ...
- 【kd-tree】bzoj2648 SJY摆棋子
#include<cstdio> #include<cmath> #include<algorithm> using namespace std; #define ...
- Android性能优化方法(四)
在一个应用程序中,一般都会存在多个Activity,每个Activity对应着一个UI布局文件.一般来说,为了保持不同窗口之间的风格统一,在这些UI布局文件中,几乎肯定会用到很多相同的布局.如果我们在 ...
- rsync 配置
1.安装rsync和配置文件 root@localhost ~]# yum -y install rsync #安装rsync服务 #CentOS 默认以 xinetd 方式运行 rsync 服务.r ...
- Zynga 开源其服务器性能监控分析工具 zPerfmon
流年不利的社交游戏服务商Zynga近日在官方博客宣布开源zPerfmon——该工具负责监控Zynga数以千计的社交游戏服务器.zPerfmon的源代码目前已经上传至Github. 包括Facebook ...
- ( 转)基于.NET平台常用的框架整理
自从学习.NET以来,优雅的编程风格,极度简单的可扩展性,足够强大开发工具,极小的学习曲线,让我对这个平台产生了浓厚的兴趣,在工作和学习中也积累了一些开源的组件,就目前想到的先整理于此,如果再想到,就 ...
- Dynamic CRM 2013学习笔记(七)追踪、监控及性能优化
本文将介绍CRM的三个内容追踪.监控及性能优化.追踪是CRM里一个很有用的功能,它能为我们的CRM调试或解决错误.警告提供有价值的信息:我们可以用window的性能监控工具来了解CRM的性能状况:最后 ...
- AutoMapper中的Map和DynamicMap——高手注重细节,思考和总结
近日在做项目的时候,遇到了个怪问题,关于AutoMapper的细节问题,也是不为一般人所关注的. 本人研究AutoMapper也没有多长时间,而且研究的过程中也写了关于AutoMapper的系列基础教 ...