【LeetCode】Integer to Roman(整数转罗马数字)
这道题是LeetCode里的第12道题。
吐了,刚做完“罗马数字转整数”,现在又做这个。这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表。但哈希表我还不熟练啊。先拿if嵌套练练手。
题目说道:
罗马数字包含以下七种字符:
I,V,X,L,C,D和M。字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000例如, 罗马数字 2 写做
II,即为两个并列的 1。12 写做XII,即为X+II。 27 写做XXVII, 即为XX+V+II。通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做
IIII,而是IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为IX。这个特殊的规则只适用于以下六种情况:
I可以放在V(5) 和X(10) 的左边,来表示 4 和 9。X可以放在L(50) 和C(100) 的左边,来表示 40 和 90。C可以放在D(500) 和M(1000) 的左边,来表示 400 和 900。给定一个整数,将其转为罗马数字。输入确保在 1 到 3999 的范围内。
示例 1:
输入: 3
输出: "III"示例 2:
输入: 4
输出: "IV"示例 3:
输入: 9
输出: "IX"示例 4:
输入: 58
输出: "LVIII"
解释: L = 50, V = 5, III = 3.
示例 5:
输入: 1994
输出: "MCMXCIV"
解释: M = 1000, CM = 900, XC = 90, IV = 4.
代码有点恐怖,就跟楼梯一样还挺好看的233333333
代码如下:
class Solution {
public:
string intToRoman(int num) {
string roman;
while(num){
if(num>=1000){num-=1000;roman+='M';}
else{
if(num>=900){num-=900;roman+='C';roman+='M';}
else{
if(num>=500){num-=500;roman+='D';}
else{
if(num>=400){num-=400;roman+='C';roman+='D';}
else{
if(num>=100){num-=100;roman+='C';}
else{
if(num>=90){num-=90;roman+='X';roman+='C';}
else{
if(num>=50){num-=50;roman+='L';}
else{
if(num>=40){num-=40;roman+='X';roman+='L';}
else{
if(num>=10){num-=10;roman+='X';}
else{
if(num>=9){num-=9;roman+='I';roman+='X';}
else{
if(num>=5){num-=5;roman+='V';}
else{
if(num>=4){num-=4;roman+='I';roman+='V';}
else{num-=1;roman+='I';}
}
}
}
}
}
}
}
}
}
}
}
}
return roman;
}
};
//你可以换个方式看23333
/*
class Solution {
public:
string intToRoman(int num) {
string roman;
while(num){
if(num>=1000){num-=1000;roman+='M';}
else{if(num>=900){num-=900;roman+='C';roman+='M';}
else{if(num>=500){num-=500;roman+='D';}
else{if(num>=400){num-=400;roman+='C';roman+='D';}
else{if(num>=100){num-=100;roman+='C';}
else{if(num>=90){num-=90;roman+='X';roman+='C';}
else{if(num>=50){num-=50;roman+='L';}
else{if(num>=40){num-=40;roman+='X';roman+='L';}
else{if(num>=10){num-=10;roman+='X';}
else{if(num>=9){num-=9;roman+='I';roman+='X';}
else{if(num>=5){num-=5;roman+='V';}
else{if(num>=4){num-=4;roman+='I';roman+='V';}
else{num-=1;roman+='I';}}}}}}}}}}}}}return roman;
}
};
*/
我数数了数总共12层if。还行,没有难度!
看那些比我快的都是使用哈希表,或者是使用千百十位从大到小分别判断,循环,遍历。
当然,也有不少用判断语句的,if+else if+else或者switch+case。但像这样if+else到底的只有我一个吧。
提交结果:

个人总结:
题目不难,不想写if语句的话可以用表映射。代码如下:
class Solution {
public:
string m[4][10] = \
{{"","M","MM","MMM"},
{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"},
{"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"},
{"","I","II","III","IV","V","VI","VII","VIII","IX"}};
int d[4] = {1000,100,10,1};
string intToRoman(int num) {
string ans = "";
int i = 0;
for (auto v : d){
ans += m[i++][num/v];
num %= v;
}
return ans;
}
};
static const auto __false_nullptr = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return nullptr;
}();
二维数组m就是表格。每一行分别代表千、百、十、个位。也是一种比较好的解题方法。来源:Click
【LeetCode】Integer to Roman(整数转罗马数字)的更多相关文章
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- lintcode :Integer to Roman 整数转罗马数字
题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】12. Integer to Roman 整数转罗马数字
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ...
- [leetcode]12. Integer to Roman整数转罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Leetcode12.Integer to Roman整数转罗马数字
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...
- [LintCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. The number is guaranteed to be within the range fro ...
- LeetCode: Integer to Roman 解题报告
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- Leetcode12--->Integer to Roman(整数转换为罗马数字)
题目: 给定一个整数,将其转换为罗马数字; 题目很简单,主要是依靠整数和罗马数字的对应表: I= 1:V= 5: X = 10: L = 50: C = 100: D = 500: M = 1000 ...
随机推荐
- linux安装redis官方教程
官方链接:http://redis.io/download Download, extract and compile Redis with: $ wget http://download.redis ...
- Android 滑动RecyclerView时隐藏部分控件
在使用RecyclerView控件时,上下拖动控件时的时候,需要实时的隐藏与显示部分控件,已到达很好的用户体验. 原理很简单,当RecyclerView拖动至最上层时显示控件,当RecyclerV ...
- [Python]输出中文报错的解决方法
问题现象:在PyCharm工具编辑python语句输出中文时,程序报错. 解决方法(2种): 1.在代码开头加#coding=utf-8(注意要加#) 2.还是在代码开头加#-*- coding: u ...
- kettle数据同步方法
1.实时性要求不高,采用全删全插的方式(适合于维度表.大数据量表) 2.有时间维度,直接从事实表同步的数据,可以采用根据时间字段进行筛选,增量同步.这个网上有很多例子,就不重复写了. 3.没有时间维度 ...
- history 路由且带二级目录的Apache配置
有多个项目目录的时候 由于项目不知一个,所以不得不为每一个项目建一个专有的文件夹,这就导致了在配置nginx的时候会出现二级目录 - step1: 修改 vue.config.js 添加配置 ...
- No-11.变量进阶
变量进阶 目标 变量的引用 可变和不可变类型 局部变量和全局变量 01. 变量的引用 变量 和 数据 都是保存在 内存 中的 在 Python 中 函数 的 参数传递 以及 返回值 都是靠 引用 传递 ...
- SSI的实例(登录增删改查)
源码下载:http://download.csdn.net/detail/u011518709/8195143 主要jar包: 配置文件:web.xml <?xml version=" ...
- 697. Degree of an Array@python
Given a non-empty array of non-negative integers nums, the degree of this array is defined as the ma ...
- 2018 CCF NOIP提高组&&普及组答案
答案: 这是今年的答案大家觉得能进到复赛吗? 下一篇文章将会为大家推荐我自己出的复赛题!!!
- 日志平台-ELK6.4
一.环境 linux-node1 192.168.127.201 linux-node2 192.168.127.202 centos7.3 elasticsearch6.4 logstash6.4 ...