【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 the range from 1 to 3999.
逐区间做处理。
解法一:非递归
class Solution {
public:
string intToRoman(int num) {
string ret;
//M<-->1000
while(num >= )
{
ret += 'M';
num -= ;
}
//to here, num < 1000
//CM<-->900
if(num >= )
{
ret += "CM";
num -= ;
}
//to here, num < 900
//D<-->500
if(num >= )
{
ret += 'D';
num -= ;
}
//to here, num < 500
if(num >= )
{
ret += "CD";
num -= ;
}
//to here, num < 400
//C<-->100
while(num >= )
{
ret += 'C';
num -= ;
}
//to here, num < 100
//XC<-->90
if(num >= )
{
ret += "XC";
num -= ;
}
//to here, num < 90
//L<-->50
if(num >= )
{
ret += 'L';
num -= ;
}
//to here, num < 50
//XL<-->40
if(num >= )
{
ret += "XL";
num -= ;
}
//to here, num < 40
//X<-->10
while(num >= )
{
ret += 'X';
num -= ;
}
//to here, num < 10
//IX<-->9
if(num >= )
{
ret += "IX";
num -= ;
}
//to here, num < 9
//V<-->5
if(num >= )
{
ret += 'V';
num -= ;
}
//to here, num < 5
//IV<-->4
if(num >= )
{
ret += "IV";
num -= ;
}
//to here, num < 4
//I<-->1
while(num >= )
{
ret += 'I';
num -= ;
}
return ret;
}
};
解法二:递归
class Solution {
public:
string intToRoman(int num) {
if(num >= )
return "M" + intToRoman(num-);
else if(num >= )
return "CM" + intToRoman(num-);
else if(num >= )
return "D" + intToRoman(num-);
else if(num >= )
return "CD" + intToRoman(num-);
else if(num >= )
return "C" + intToRoman(num-);
else if(num >= )
return "XC" + intToRoman(num-);
else if(num >= )
return "L" + intToRoman(num-);
else if(num >= )
return "XL" + intToRoman(num-);
else if(num >= )
return "X" + intToRoman(num-);
else if(num >= )
return "IX" + intToRoman(num-);
else if(num >= )
return "V" + intToRoman(num-);
else if(num >= )
return "IV" + intToRoman(num-);
else if(num >= )
return "I" + intToRoman(num-);
else
return "";
}
};
【LeetCode】12. Integer to Roman (2 solutions)的更多相关文章
- 【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 Integer to Roman
一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- 【LeetCode】012. Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 【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 wit ...
- 《LeetBook》leetcode题解(12):Integer to Roman[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
随机推荐
- Android.mk中引用第3方动态库
Android.mk 文件内容: LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS := optional LOC ...
- MVC 部署在IIS7 出现的 404 错误
如果你不幸在 windows server 2008 R2 的 IIS7 中部署 MVC 站点的话,如果你输入:http://yourdomain/Organization/Index ,那么你很有可 ...
- Flask 学习(三)模板
Flask 学习(三)模板 Flask 为你配置 Jinja2 模板引擎.使用 render_template() 方法可以渲染模板,只需提供模板名称和需要作为参数传递给模板的变量就可简单执行. 至于 ...
- 理解HTML5数据推送应用开发问题
一.数据推送 SSE是一种允许服务端向客户端推送新数据(通常称作数据推送)的HTML5技术.那么,究竟什么是数据推送?它与我们可能用过的其他技术有什么不同呢? 让我先来回答什么不是数据推送.数据推送有 ...
- MySql无法远程登录以及IP被锁解决办法
授权 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '' WITH GRANT OPTION;Query OK, 0 rows aff ...
- Cognos报表展示图片小技巧
场景:在销售行业,比如手机,服装行业,如果仅仅的显示数字.文字那就显得不是很生动了,例如可以显示一下图片,那种样子的产品受大家喜欢. 样例1:在报表头都喜欢加上一些公司的logo,让报表看上去专业点. ...
- uva 213 - Message Decoding (我认为我的方法要比书上少非常多代码,不保证好……)
#include<stdio.h> #include<math.h> #include<string.h> char s[250]; char a[10][250] ...
- JS计算本周一和本周五的日期
代码不长: var today=new Date();var weekday=today.getDay(); var monday=new Date(1000*60*60*24*(1-weekd ...
- USACO humble
用set构造,优先队列和堆也能够 /* ID:kevin_s1 PROG:humble LANG:C++ */ #include <iostream> #include <cstdi ...
- UVa 10820 - Send a Table
题目:找到整数区间[1.n]中全部的互质数对. 分析:数论,筛法,欧拉函数.在筛素数的的同一时候.直接更新每一个数字的欧拉函数. 每一个数字一定会被他前面的每一个素数筛到.而欧拉函数的计算是n*π(1 ...