【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 Pro] Swift 3.0多线程
本文只介绍Grand Central Dispath(GCD) 中央调度 个人认为一个GCD就够用了,可能是改版或是其他的在找之前写的多线程方法时发现不能用了,看文档之后发现改了,现在看上去更加简单易 ...
- 定义和使用EL函数
EL为表达式语言,在EL中,允许定义和使用函数.下面将介绍如何定义和使用EL的函数. 1. 定义和使用函数 函数的定义和使用分为以下3个步骤: (1)编写一个Java类,并在该类中编写公用的静态方法, ...
- go语言之进阶篇结构体指针类型匿名字段
1.结构体指针类型匿名字段 示例: package main import "fmt" type Person struct { name string //名字 sex byte ...
- IOS基本数据类型之枚举
枚举是C语言中的一种基本数据类型,通过枚举可以声明一组常数,来代表不同的含义,它实际上就是一组整型常量的集合. 枚举是非常常用的一种类型,在现实生活中也很常见.比如有四个季节,在不同的季节需要显示不同 ...
- Kettle资源库采用SQLserver数据库需要注意的点
Kettle开源ETL工具有着自己的元数据存储方式,可以分为两种 1:File 2:DB 文件存储我这里就不多说了,下面说一下在用SQLserver2008 R2作为资源库在创建的过程中遇到的问题 K ...
- (转)[unity3d]easytouch的使用
对于移动平台上的RPG类的游戏,我们常用虚拟摇杆来控制人物角色的行走和一些行为,相信我们对它并不陌生,之前尝试了EasyTouch2.5,发现并没有最新版的3.1好用,2.5版本的对于自适应没有做的很 ...
- 使用Zxing开发Air版二维码扫描工具
简介实现的核心要点和几个须要注意的问题: 使用开源类库:Zxing,微信也是用的这个.下载地址:http://code.google.com/p/zxing/ as版:https://github.c ...
- XTU1236 Fraction
Fraction Accepted : 124 Submit : 806 Time Limit : 1000 MS Memory Limit : 65536 KB Fraction Problem D ...
- C#类似版本号有多个分割符可以产生的排列组合,类似版本号比较
我采用asp.net进行演示 送给有缘人吧,可以获得类似版本号的功能,也可以对比两个版本号,我这里是其他需要用逗号分割的 using System; public partial class _Def ...
- Appium Python 六:管理应用和Activity
管理应用 1. 将当前应用放到后台 执行之后,应用会被放到后台特定时间.比如这里就是5秒,5秒过后,应用会重新回到前台. driver.background_app(5) 官网示例: driver.b ...