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的更多相关文章

  1. 【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 ...

  2. 【LeetCode】12. Integer to Roman 整数转罗马数字

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...

  3. 【LeetCode】12. Integer to Roman 整型数转罗马数

    题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...

  4. 【leetcode】12. Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  5. 【LeetCode】12. 整数转罗马数字

    12. 整数转罗马数字 知识点:字符串 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 100 ...

  6. 【LeetCode】397. Integer Replacement 解题报告(Python)

    [LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...

  7. 【LeetCode】面试题13. 机器人的运动范围

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  8. 【LeetCode】386. Lexicographical Numbers 解题报告(Python)

    [LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. 【LeetCode】数组--合并区间(56)

    写在前面   老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...

随机推荐

  1. php多维数组化一维数组

    一.使用foreach <?php function arr_foreach ($arr) { static $tmp=array(); if (!is_array ($arr)) { retu ...

  2. java实现附件预览(openoffice+swftools+flexpaper)

    先附上本人参考的文章,基于的 flexpaper版本 为 1.5,本人由于使用的是 2.1.9 ,故之后说明: 已经支持加载中文文件名 代码下载 1.概述 主要原理 1.通过第三方工具openoffi ...

  3. Android里的多线程知识点

    1.Thread类与Runnable接口 子类继承Thread类实现跑自己逻辑的run方法,在调用Thread类的start方法后,会自动调用run方法,该对象只可以调用一次start方法,即Thre ...

  4. 命令 crontab

    crontab命令选项基本只有对用户操作的选项: -u 指定一个用户 -l 列出某个用户的任务计划 -r 删除某个用户的任务 -e 编辑某个用户的任务 所以,要查看所有用户的,只能根据/etc/pas ...

  5. [转]SQL语句优化技术分析

    一.操作符优化 1.IN 操作符 用IN写出来的SQL的优点是比较容易写及清晰易懂,这比较适合现代软件开发的风格.但是用IN的SQL性能总是比较低的,从Oracle执行的步骤来分析用IN的SQL与不用 ...

  6. java 反编译插件 JD-Eclipse 和 JD-IntelliJ

    去官网 : http://jd.benow.ca/ 找到 JD-Eclipse 和  JD-IntelliJ,下载,前者安在eclipse上,后者安在as上.

  7. NDK(10)Android.mk各属性简介,Android.mk 常用模板

    参考 : http://blog.csdn.net/hudashi/article/details/7059006 本文内容: Android.mk简介, 各属性表, 常用Android.mk模板 1 ...

  8. Git基础(三)

    本章 就开始和大家一起学习第三块内容:远程仓储的使用操作.要参与任何一个 Git 项目的协作,必须要了解该如何管理远程仓库.远程仓库是指托管在网络上的项目仓库,可能会有好多个,其中有些你只能读,另外有 ...

  9. Android之项目推荐使用的第三方库

    1. 使用上拉更多,下拉刷新:https://github.com/JosephPeng/XListView-Android 这个是github上面更为火爆的:https://github.com/c ...

  10. 如何将开源项目部分代码作为private放在github上?

    很多时候,你的一些项目本身都是开源的,但是基于该开源项目,你可能做了部分更有价值的工作,或者由于其他原因,你不希望将这部分代码放到public上,那么有以下简单方法: 1. 创建一个private b ...