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 ...
随机推荐
- oracle 中文乱码---查看和修改客户端字符集
客户端NLS_LANG的设置方法 Windows: # 常用中文字符集set NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK # 常用unicode字符集 set ...
- homework 11 2016 5 13 读入文件做输入
#include <iostream>#include <fstream> using namespace std; int main(){ string x, y, z; c ...
- 【堆】【kd-tree】bzoj2626 JZPFAR
用堆记录答案.看看当前点是否比堆顶更优. #include<cstdio> #include<queue> #include<cstring> #include&l ...
- POJ 3468 A Simple Problem with Integers (线段树)
题意:给定两种操作,一种是区间都加上一个数,另一个查询区间和. 析:水题,线段树. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024 ...
- JSHint 使用说明
SHint介绍 翻译自www.jshint.comJSHint(注意不是jslint:))是一个由javascript社区驱动开发的用于检查javascript代码错误和问题的工具,有了他,可以使你保 ...
- C#开发中使用Npoi操作excel实例代码
C#开发中使用Npoi操作excel实例代码 出处:西西整理 作者:西西 日期:2012/11/16 9:35:50 [大 中 小] 评论: 0 | 我要发表看法 Npoi 是什么? 1.整个Exce ...
- 1.1 Activity
1.概念 Application:由多个相关的松散的与用户进行交互Activity组成,通常被打包成apk后缀文件中: Activity:就是被用来进行与用户交互和用来与android内部特性交互的组 ...
- 1.4 Service
用于在后台完成用户指定的操作,为其他组件提供后台服务或监控其他组件的运行状态. 开发人员需要在应用程序配置文件中声明全部的service,使用<service></service&g ...
- sql 过了试用期不能启动的,修改时间启动后还原。
@echo off set nowtime=%date% echo 2014-12-01|date sc start MSSQLSERVER ping -n 5 127.1&g ...
- 打印出所有的 "水仙花数 ",所谓 "水仙花数 "是指一个三位数 其各位数字立方和等于该数本身。 例如:153是一个 "水仙花数 " 因为153=1*1*1+5*5*5+3*3*3
for (int i = 100; i <= 999; i++) { int geWei, shiWei, baiWei; baiWei = i / 100; shiWei = (i - bai ...