【LeetCode】012. Integer to Roman
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的更多相关文章
- 【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 ...
- 【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】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 【一天一道LeetCode】#12 Integer to Roman
一天一道LeetCode系列 (一)题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- 【LeetCode】Reverse Integer(整数反转)
这道题是LeetCode里的第7道题. 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转. 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 ...
- 【LeetCode】343. Integer Break 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学解法 动态规划 日期 题目地址:https:// ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
随机推荐
- 安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决!
安卓编译出错: Process 'command 'C:\Java\jdk1.8.0_51\bin\java.exe'' finished with non-zero exit value 1 解决! ...
- OSI模型第三层网络层-初识路由协议
1.路由协议: 顾名思义就是路由器所使用的协议. 分类: (1)按照作用范围分类,IGP(类型)内部网关协议(rip,ospf,isis),EGP(类型)边界路由协议(bgp) 把互联网比作整个世界土 ...
- POJ 1845-Sumdiv【经典数学题目---求因子和】
转载请注明出处:http://blog.csdn.net/lyy289065406/article/details/6648539 優YoU http://user.qzone.qq.com/289 ...
- VS2015增量编译,加快编译速度
起因:之前工程设置的好好的, 改动一个文件,必定是只编译该文件相关的.然而最近就是无论是否改动文件,都会有部分文件重新编译. 解决流程:查看增量编译的设置1.1 因为工程是在Debug模式下,so清空 ...
- 9.Django里的数据同步migrations命令
一个关键的目录: 目录名:migrations 作用:用来存放通过makemigrations命令生成的数据库脚本,这里的内容一般不要手动去改 规定:app目录下必须要有migrations目录且目录 ...
- JPA hibernate spring repository pgsql java 工程(二):sql文件导入数据,测试数据
使用jpa保存查询数据都很方便,除了在代码中加入数据外,可以使用sql进行导入.目前我只会一种方法,把数据集中在一个sql文件中. 而且数据在导入中常常具有先后关系,需要用串行的方式导入. 第一步:配 ...
- js实现粘贴板复制
<a href = '#' onclick ='javascript:window.clipboardData.setData('text','${form.param}');alert('クリ ...
- (转)Javascript模块化编程(三):require.js的用法
这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用require.js? 最早的 ...
- Linux 3 -grep
七. grep家族: 1. grep退出状态: 0: 表示成功: 1: 表示在所提供的文件无法找到匹配的pattern: 2: 表示参数中提供的文件不存在. 见如下示例: /> grep 'ro ...
- STM32 ~ J-LINK V8 修复
1.1 安装固件烧录软件 ♦请ATMEL官方网址下载AT91-ISP下载软件. 软件下载地址:http://www.atmel.com/dyn/products/tools_card.asp?t ...