【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刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
随机推荐
- 在eclipse中调试web项目的时候如何把web项目分配给配置好的服务器
举个例子,我今天在做spring和struts2整合的例子 新建项目blk 1.配置好web.xml,struts.xml,applicationContext.xml,写好jsp页面 2.把stru ...
- OkHttp使用进阶(译自OkHttp官方教程)
没有使用过OkHttp的,可以先看OkHttp使用介绍 英文版原版地址 Recipes · square/okhttp Wiki 同步get 下载一个文件,打印他的响应头,以string形式打印响应体 ...
- Linux驱动修炼之道-RTC子系统框架与源码分析【转】
转自:http://helloyesyes.iteye.com/blog/1072433 努力成为linux kernel hacker的人李万鹏原创作品,为梦而战.转载请标明出处 http://bl ...
- Linux守护进程详解(init.d和xinetd) [转]
一 Linux守护进程 Linux 服务器在启动时需要启动很多系统服务,它们向本地和网络用户提供了Linux的系统功能接口,直接面向应用程序和用户.提供这些服务的程序是由运行在后台 的守护进程来执行的 ...
- tomcat7 1000并发量配置 tomcat7配置优化
修改tomcat/conf/server.xml配置文件. <Executor name="tomcatThreadPool" namePrefix="catali ...
- 15.导入网表及status介绍[原创]
一.导入网表 在导入网表之前你的封装需确认是在你的封装路径下 建立Board工程后: ① ② ③ ④放置器件 ⑤ (切记,封装路径一定要添加) 二.status介绍 --- (常用) -------- ...
- 14.allegro.PCB设计前工作[原创]
一.设置板子大小 -- ----- 板子边框 2种设置outline方法,创建2个KI,两个keepin,,r:允许布线区:p允许摆放元件的区域 法一:直接添加线 ①Board Geometry(最外 ...
- hdu - 1180 诡异的楼梯 (bfs+优先队列)
http://acm.hdu.edu.cn/showproblem.php?pid=1180 注意点就是楼梯是在harry移动完之后才会改变方向,那么只要统计到达这个点时间奇偶性,就可以知道当前楼梯是 ...
- How to: Run Tests from Microsoft Visual Studio
https://msdn.microsoft.com/en-us/library/ms182470.aspx Running Automated Tests in Visual Studio Visu ...
- create-maximum-number(难)
https://leetcode.com/problems/create-maximum-number/ 这道题目太难了,花了我很多时间.最后还是参考了别人的方法.还少加了个greater方法.很难. ...