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. Windows7 配置两个版本的java环境,可自由切换

    1. 准备工作 下载jdk: jdk1.7[http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads ...

  2. poj2075

    Tangled in Cables Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6348   Accepted: 2505 ...

  3. 成功扩展live555支持ipv6,同时支持RTSPServer & RTSPClient

    live555对ipv6的扩展 从live555的官网看live555的发展历史,实在是历史悠久,保守估计已经发展了至少16年以上了,同时,这也导致了live555在很多架构和考虑上面不能满足现代化的 ...

  4. windowsphone8.1学习笔记之应用数据(四)

    应用数据的存储格式常用的分为json和xml两种(其实我都想略过这个地方的,json我一直用的是json.net,而wp上操作xml的方式与其他相比也没太多变化). 先说说json数据存储,关于jso ...

  5. isinstance/issubclass/type的区别?

    type() 判断某个对象是否是该类创建的,只看一层,如果是继承类,也不会考虑继承类的类型.. Issubclass() 判断该类是否是另一个类的派生类,也就是子类,参数为类. isinstance( ...

  6. PAT 1063. 计算谱半径(20)

    在数学中,矩阵的“谱半径”是指其特征值的模集合的上确界.换言之,对于给定的n个复数空间的特征值{a1+b1i, ..., an+bni},它们的模为实部与虚部的平方和的开方,而“谱半径”就是最大模. ...

  7. linux c编程:线程互斥一

    当多个线程共享相同的内存的时候,需要确保每个线程都看到一致的数据视图.如果每个线程使用的变量都是其他线程不会读取和修改的.那么就不存在一致性问题.同样,如果变量是只读的,多个线程也不会有一致性的问题. ...

  8. FlashFXP上传下载

    正常情况我们的生产环境我们本地是不能直接访问的,因为网段不通,且生产环境不允许我们随便访问,但是对于我们自运维的集群我们有时候需要上去做一些操作,通过堡垒机跳转到生产机器上即可,但是我们不能通过xsh ...

  9. 关于date和String互相转换的问题

    其实原理很简单,就是将String类型的变量使用SimpleDateFormat来转换成Date,然后用getTime()方法比较 SimpleDateFormat sdf = new SimpleD ...

  10. xutils3文件上传、下载、get、post请求

    @ContentView(R.layout.activity_xutils3_net) public class XUtils3NetActivity extends Activity { @View ...