【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:// ...
随机推荐
- 关于VS 工具箱灰色,不可用的解决方案
使用vs的命令行工具,在命令行中运行:devenv /ResetSkipPkgs ,重新打开vs,重置一下工具箱 ,OK,成功了~! 希望能对大家有帮助!
- iOS开发-多线程NSOperation和NSOperationQueue
上一篇文章稍微提及了一下NSThread的使用,NSThread能直观地控制线程对象,不过需要自己管理线程的生命周期,线程同步,用起来比较繁琐,而且比较容易出错.不过Apple给出了自己的解决方案NS ...
- 【右滑返回】滑动冲突 Scroller DecorView
基本思想 我们的滑动逻辑主要是利用View的scrollBy() 方法, scrollTo()方法和Scroller类来实现的 当手指拖动视图的时候,我们监听手指在屏幕上滑动的距离 利用View的sc ...
- JSP简单练习-定时刷新页面
<%@ page contentType="text/html; charset=gb2312" %> <%@ page import="java.ut ...
- (转)PlayerPrefs游戏存档
unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs.工作原理非常简单,以键值对的形式将数据保存在文件中,然后程序可以根据这个名称取出上次保存的数值. PlayerPr ...
- Linux上安装Bugzilla4.4小记
因项目需要,我受命在一台Linux服务器上搭建一个Bugzilla,进过一天的调试,这项任务总算完成了.现在可以肯定的说,安装过程不复杂,基本就是解压,填参数,执行命令.Bugzilla要跑起来,本机 ...
- spring常用注解以IOC理解
使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...
- js 动画性能分析 transfrom
1.动画实现代码 (1)使用定位实现: <!DOCTYPE html> <html> <head> <meta charset="utf-8&quo ...
- Codeforces Round #228 (Div. 1) C 贪心
嘎嘎,今天被一些事耽误了,可是还是A了几个题目,这道题还不错 题目链接: 题意:两个人玩游戏,有N堆纸牌,纸牌上有数字,A每次仅仅能取N堆中的 当中一个的顶部的 纸牌,B仅仅能取N堆中的当中一个底部 ...
- STL之lambda表达式
一.简介 我们可以向一个算法传递任何类别的可调用对象.对于一个对象或一个表达式,如果可以对其使用调用运算符,则称它为可调用的.即,如果e是一个可调用的表达式,则我们可以编写代码e(args),其中ar ...