【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 within the range from 1 to 3999.
Solution: 枚举,考虑每个字符以及每两个字符的组合

1 class Solution {
2 public:
3 string intToRoman(int num) { //runtime:28ms
4 string ret;
5 //M<-->1000
6 while(num >= 1000)
7 {
8 ret += 'M';
9 num -= 1000;
10 }
11 //to here, num < 1000
12 //CM<-->900
13 if(num >= 900)
14 {
15 ret += "CM";
16 num -= 900;
17 }
18 //to here, num < 900
19 //D<-->500
20 if(num >= 500)
21 {
22 ret += 'D';
23 num -= 500;
24 }
25 //to here, num < 500
26 if(num >= 400)
27 {
28 ret += "CD";
29 num -= 400;
30 }
31 //to here, num < 400
32 //C<-->100
33 while(num >= 100)
34 {
35 ret += 'C';
36 num -= 100;
37 }
38 //to here, num < 100
39 //XC<-->90
40 if(num >= 90)
41 {
42 ret += "XC";
43 num -= 90;
44 }
45 //to here, num < 90
46 //L<-->50
47 if(num >= 50)
48 {
49 ret += 'L';
50 num -= 50;
51 }
52 //to here, num < 50
53 //XL<-->40
54 if(num >= 40)
55 {
56 ret += "XL";
57 num -= 40;
58 }
59 //to here, num < 40
60 //X<-->10
61 while(num >= 10)
62 {
63 ret += 'X';
64 num -= 10;
65 }
66 //to here, num < 10
67 //IX<-->9
68 if(num >= 9)
69 {
70 ret += "IX";
71 num -= 9;
72 }
73 //to here, num < 9
74 //V<-->5
75 if(num >= 5)
76 {
77 ret += 'V';
78 num -= 5;
79 }
80 //to here, num < 5
81 //IV<-->4
82 if(num >= 4)
83 {
84 ret += "IV";
85 num -= 4;
86 }
87 //to here, num < 4
88 //I<-->1
89 while(num >= 1)
90 {
91 ret += 'I';
92 num -= 1;
93 }
94 return ret;
95 }
96 };

13 - Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Analysis:
| 基本字符 |
I
|
V
|
X
|
L
|
C
|
D
|
M
|
|---|---|---|---|---|---|---|---|
| 相应的阿拉伯数字表示为 |
1
|
5
|
10
|
50
|
100
|
500
|
1000
|
- 个位数举例Ⅰ-1、Ⅱ-2、Ⅲ-3、Ⅳ-4、Ⅴ-5、Ⅵ-6、Ⅶ-7、Ⅷ-8、Ⅸ-9
- 十位数举例Ⅹ-10、Ⅺ-11、Ⅻ-12、XIII-13、XIV-14、XV-15、XVI-16、XVII-17、XVIII-18、XIX-19、XX-20、XXI-21、XXII-22、XXIX-29、XXX-30、XXXIV-34、XXXV-35、XXXIX-39、XL-40、L-50、LI-51、LV-55、LX-60、LXV-65、LXXX-80、XC-90、XCIII-93、XCV-95、XCVIII-98、XCIX-99
- 百位数举例C-100、CC-200、CCC-300、CD-400、D-500、DC-600、DCC-700、DCCC-800、CM-900、CMXCIX-999
- 千位数举例M-1000、MC-1100、MCD-1400、MD-1500、MDC-1600、MDCLXVI-1666、MDCCCLXXXVIII-1888、MDCCCXCIX-1899、MCM-1900、MCMLXXVI-1976、MCMLXXXIV-1984、MCMXC-1990、MM-2000、MMMCMXCIX-3999
Solution 1: 借助map
class Solution {
public:
int romanToInt(string s) { //runtime:76ms
int ret=;
map<char,int> m;
m['I']=;m['V']=;m['X']=;m['L']=;m['C']=;m['D']=;m['M']=;
for(int i=;i<s.size();i++){
if(m[s[i]]<=m[s[i-]])ret+=m[s[i-]];
else
ret-=m[s[i-]];
}
ret+=m[s[s.size()-]];
return ret;
}
};
Solution 2: 每个字符前的字符确定,C前只可能是C或X,M前只可能是M或C
class Solution {
public:
int romanToInt(string s) { //runtime: 36ms
int n = ;
char lastC = ;
for(int i = ; i < s.size(); i ++)
{
switch(s[i])
{
case 'I':
n += ;
lastC = s[i];
break;
case 'V':
if(lastC == 'I')
{//IV
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'X':
if(lastC == 'I')
{//IX
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'L':
if(lastC == 'X')
{//XL
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'C':
if(lastC == 'X')
{//XC
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'D':
if(lastC == 'C')
{//CD
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
case 'M':
if(lastC == 'C')
{//CM
n -= ;
n += ;
lastC = ;
}
else
{
n += ;
lastC = s[i];
}
break;
default:
return ;
}
}
return n;
}
};
【LeetCode】12 & 13 - Integer to Roman & Roman to Integer的更多相关文章
- 【LeetCode】12. Integer to Roman (2 solutions)
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- 【LeetCode】12. Integer to Roman 整型数转罗马数
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- 【leetcode】12. Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- 【LeetCode】12. 整数转罗马数字
12. 整数转罗马数字 知识点:字符串 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 100 ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 【LeetCode】面试题13. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】数组--合并区间(56)
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
随机推荐
- Android 时间轴
最近开发的app中要用到时间轴这东西,需要实现的效果如下: 想想这个东西应该可以用listview实现吧.然后最近就模拟着去写了: 首先写 listview的item的布局: listview_it ...
- RichLabel基于Cocos2dx+Lua v3.x
RichLabel 简介 RichLabel基于Cocos2dx+Lua v3.x解析字符串方面使用了labelparser,它可以将一定格式的字符串,转换为lua中的表结构扩展标签极其简单,只需添加 ...
- HDU 4000 Fruit Ninja 树状数组 + 计数
给你N的一个排列,求满足:a[i] < a[k] < a[j] 并且i < j < k的三元组有多少个. 一步转化: 求出所有满足 a[i] < a[k] < a[ ...
- C# 文本文件打印类库(C#)
我写了一个打印文本文件的类库,功能包括:打印预览.打印.打印时可以选择打印机,可以指定页码范围.调用方法非常简单:TextFilePrinter p = new TextFilePrinter(tbx ...
- framework-Binder
init进程fork servicemanager进程用来提供(server)注册service和(client)检索service功能.servicemanager维护了一个service列表,cl ...
- Support Library(5)在eclipse中导入SupportXXXDemos
Support4Demos只用一个v4.,Support7Demos只要v13.jar, SupportAppNavigation只要一个v4.jar. Support7Demos 需要资源全部v7系 ...
- javascript callback函数的理解与使用
最近做的一个项目中用到了callback函数,于是就研究了下总结下我对javascript callback的理解 首先从callback的字面翻译“回调” 可以理解这是一个函数被调用的机制 当我们遇 ...
- HDU 4655 Cut Pieces(数学分析题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4655 题意:给出n个石子,第i个石子可以染ai种颜色.对于每种颜色,比如颜色1221,我们称有3段.连 ...
- 《OD学hive》第五周0723
https://cwiki.apache.org/confluence/display/Hive/LanguageManual 一.创建表 create table student(id int, n ...
- source idea of Unit
After the construction of Global environment setting code, there is a convenient way for us in the f ...