Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

题解:

  观察 1 到 10 :Ⅰ,Ⅱ,Ⅲ,Ⅳ(IIII),Ⅴ,Ⅵ,Ⅶ,Ⅷ,Ⅸ,Ⅹ,Ⅺ

  难点在于出现字符不能连续超过三次,以及大数左边至多有一个小数。所以要分情况:1 - 3,4,5 - 8,9。将小数在大数左边的情况摘出来。

Solution 1

  贪心策略

 class Solution {
public:
string intToRoman(int num) {
string res = "";
vector<int> weights{, , , , , , , , , , , , };
vector<string> tokens{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; int i = ;
while (num && i < weights.size()) {
while (num >= weights[i]) {
num -= weights[i];
res += tokens[i];
}
++i;
} return res;
}
};

Solution 2

 class Solution {
public:
string intToRoman(int num) {
vector<string> M = { "", "M", "MM", "MMM" };
vector<string> C = { "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM" };
vector<string> X = { "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC" };
vector<string> I = { "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX" };
return M[num / ] + C[(num % ) / ] + X[(num % ) / ] + I[num % ];
}
};

【LeetCode】012. Integer to Roman的更多相关文章

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

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

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

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

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

  4. 【leetcode】12. Integer to Roman

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

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

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

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

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

  7. 【LeetCode】Reverse Integer(整数反转)

    这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321  示例 2: 输入: -123 ...

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

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

  9. 【leetcode】Reverse Integer(middle)☆

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...

随机推荐

  1. u-boot下载模式LCD显示图片修改方法(基于TQ2440)

    1.明确液晶型号,这点非常重要,我手头的液晶是天嵌4.3寸屏,让人很郁闷的是液晶背面竟然写着LCD 3.5,这一点让我在上面浪费了好几个小时: 2.根据液晶型号,修改u-boot1.1.6--> ...

  2. poj1066(叉乘的简单应用)

    做完了才发现,好像没有人和我的做法一样的,不过我怎么都觉得我的做法还是挺容易想的. 我的做法是: 把周围的方框按顺时针编号,然后对于每一条边,如果点出现在边的一侧,则把另一侧所有的点加1,这样最后统计 ...

  3. UITableview刷新时界面“乱跑”现象

    Self-Sizing在iOS11下是默认开启的,Headers, footers, and cells都默认开启Self-Sizing,所有estimated 高度默认值从iOS11之前的 0 改变 ...

  4. java实现数字的反转

    例如有一个数字是:19911002,要求是,我要得到它的反转后的数:20011991 实现如下: static void reverse(int a) { int rs = 0; while (a & ...

  5. CentOS6安装DaoCloud加速器

    天朝的网,你又不是不懂.我最爱的红杏最近也用不了了.FUCK GFW. 在这,我们使用DaoCloud的加速器,打开网址 https://dashboard.daocloud.io/mirror 找到 ...

  6. QT发布的EXE打包压缩成单文件

    Enigma virtual box 是免费的软件虚拟化工具,它可以将多个文件封装到您的应用程序主文件,这样您的软件就可以制作成为单文件的绿色软件. enigma virtual box 支持所有类型 ...

  7. MainWindows

    开发带有菜单栏状态栏等常用windows应用时候使用

  8. Ubuntu 16.04更换源

    Ubuntu 16.04下载软件速度有点慢,因为默认的是从国外下载软件,那就更换到国内比较好的快速更新源(就是这些软件所在的服务器),一般直接百度Ubuntu更新源就能出来一大堆,这时候最好是找和自己 ...

  9. float后怎么居中

    我们在用css float的时候,如何让文本水平居中真的很麻烦, [float]使div(或者其他标签)的宽度自适应其内容,但它却有个弊端:无法居中.[display:inline-block]也有同 ...

  10. 利用CocoaPods管理本地工程和发布开源框架

    发布自己三方框架 发布云端库 1.创建spec pod spec create xxx 2.编辑spec s.name:名称,pod search 搜索的关键词,注意这里一定要和.podspec的名称 ...