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

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

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

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

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

  3. 【leetcode】12. Integer to Roman

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

  4. 【一天一道LeetCode】#12 Integer to Roman

    一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...

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

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

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

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

  8. 《LeetBook》leetcode题解(12):Integer to Roman[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. 【LeetCode】343. Integer Break 解题报告(Python & C++)

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

随机推荐

  1. Pycharm 增加 run 控制台缓冲行数

    Yes, you can change the value of idea.cycle.buffer.size in bin/idea.properties under the PyCharm ins ...

  2. IOS tableview 横向滚动

    1. UITableView 设置 CGRect tableViewRect = CGRectMake(0.0, 0.0, 50.0, 320.0);self.tableView = [[UITabl ...

  3. mysql访问权限GRANT ALL PRIVILEGES ON,访问权限表

    开启远程连接:2, 修改 Mysql-Server 用户配置mysql> USE mysql; -- 切换到 mysql DBDatabase changedmysql> SELECT U ...

  4. Objective-C:NSNumber类的常见用法

    NSNumber基本数据类型包装类: // //  main.m //  04-NSNumber // //  Created by ma c on 15/8/17. //  Copyright (c ...

  5. Redhat Linux FTP配置

    文件传输协议(FTP:FileTransfer Protocol)使得主机间可以共享文件. FTP 使用 TCP 生成一个虚拟连接用于控制信息,然后再生成一个单独的 TCP 连接用于数据传输.控制连接 ...

  6. Cesium随笔(3)随鼠标实时显示经纬度坐标以及高度【转】

    在网页三维地球上进行可视化开发与经纬度坐标以及高度是分不开的,能够实时获取鼠标位置的经纬度对可视化效果有很好的帮助,Cesium当然能做到: (1)首先在里创建显示坐标的容器  样式自己调整的合适即可 ...

  7. iOS开发-xCode6(iOS 8)中应用程序图标和启动页面设置

    iOS8中设置应用程序图标跟之前没有什么变化,命名规则不变,不过至于设置启动页面,网上给的方式很多都是模棱两可的,东平西凑总算是把启动页面的图片设置成功了,iOS设置启动图片有两种方式一种是Launc ...

  8. iOS开发-UITableView常用方法

    UITableView常用来展示数据,类似于Android中的ListView,相对于Android中的ListView而言,UITableView的实现是非常简单,继承UITableViewData ...

  9. mongoDB报错Cannot find module '../build/Release/bson'

    打算用nodejs写一个blog系统,发现nodejs还是存在很多的坑.在使用mongodb时遇到如下报错问题: { [Error: Cannot find module '../build/Rele ...

  10. js中replace的用法

    replace方法的语法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正则表达式对象(RegExp)也 ...