【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 使用意图传递数据
使用意图传递数据之通用方式. 测试应用:当前页面点击button传递数据到一个新的页面显示在textview中. 首先在,mainActivity.xml文件中加入一个button按钮 <But ...
- J2ee 巴巴网站制作(一)
用户模块图:
- html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null
html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null 解决方法:使用hidden 利用javascript赋值,传递到后台
- DataGridView过滤区分大小写问题
DataTable上的过滤方法: 一.可以用DataTable.Select("条件"),返回DataRow[]格式的结果集. DataRow[] drArr = dt.Selec ...
- Support Library(4)ecliplse导入支援包的方法
准备工作 下载支援包到本地.在 <sdk>/android-sdks/extras/android/support/v7 下包含两个目录「 m2repository,support 」 ...
- MongoDB 学习笔记(二) 高级查询
1.条件运算符 2.$all 匹配所有 3.$exists 判断字段是否存在 4.NUll 值处理 5.$mod 取模处理 6.$ne 不等于 7. $in 包含,与sql用法相同 8. $nin 不 ...
- maven-source 1.3 中不支持注释请使用 -source 5 或更高版本以启用注释
解决办法:在pom里 加上以下代码 <build> <plugins> <plugin> <groupId>org.apache.maven.plugi ...
- git remotes
简单地说,一个remote repository是一个非本地的repo.它可以是在你公司网络上的另外一个git repo,也可以是在internet上,甚至在你本地文件系统中的一个repo,关键点是它 ...
- Qt之模式、非模式、半模式对话框
简述 关于"模式"和"非模式"对话框,相信大家都比较熟悉,但其中有一个可能很多人都比较陌生,介于两者之间的状态,我们称之为"半模式". 简述 ...
- UVa 10020 (最小区间覆盖) Minimal coverage
题意: 数轴上有n个闭区间[ai, bi],选择尽量少的区间覆盖一条指定线段[0, m] 算法: [start, end]为已经覆盖到的区间 这是一道贪心 把各个区间先按照左端点从小到大排序,更新st ...