[leetcode]12. Integer to Roman整数转罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
- Ican be placed before- V(5) and- X(10) to make 4 and 9.
- Xcan be placed before- L(50) and- C(100) to make 40 and 90.
- Ccan be placed before- D(500) and- M(1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example 1:
Input: 3
Output: "III"
Example 2:
Input: 4
Output: "IV"
Example 3:
Input: 9
Output: "IX"
Example 4:
Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 5:
Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Solution1: Simulation
Let's walk through an example, if input: 6, output is "LI", we find the maximum value that we can remove from 6, iteratively doing that and appending its corresponding Roman numerals into result.
Step1, For most cases, Roman Numeral use addition(相加).
Coz only a few cases using subtraction(相减) like 4 or 9, we can pre-calculate those ones.
   int values[] = {1000, 900,  500, 400, 100,  90,  50,  40,   10,   9,   5,    4,     1};
String romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
Step2, traverse the values[], find the maximum value we can remove from 6
   int values[] = {1000, 900,  500, 400, 100,  90,  50,  40,   10,   9,   5,    4,     1};
String romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
Step3, append its corresponding Roman numerals into result, remove it from 6, and jump to Step2 to do the next iteration
code:
/*
Time Complexity: O(n)
Space Complexity: O(1)
*/
class Solution {
public String intToRoman(int num) {
final int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
final String romans[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < values.length; i++){
while( num >= values[i]){
num -= values[i];
sb.append(romans[i]);
}
}
return sb.toString();
}
}
[leetcode]12. Integer to Roman整数转罗马数字的更多相关文章
- [LeetCode] 12. Integer to Roman 整数转为罗马数字
		Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ... 
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
		Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ... 
- 【LeetCode】12. Integer to Roman 整数转罗马数字
		作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:roman, 罗马数字,题解,leetcode, 力扣, ... 
- 【LeetCode】Integer to Roman(整数转罗马数字)
		这道题是LeetCode里的第12道题. 吐了,刚做完"罗马数字转整数",现在又做这个.这个没什么想法,只能想到使用if语句嵌套,或者使用哈希表.但哈希表我还不熟练啊.先拿if嵌套 ... 
- Leetcode 12——Integer to Roman
		12.Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be withi ... 
- Leetcode  12.   Integer to Roman(打表,水)
		12. Integer to Roman Medium Roman numerals are represented by seven different symbols: I, V, X, L, C ... 
- LeetCode 12 Integer to Roman (整数转罗马数字)
		题目链接: https://leetcode.com/problems/integer-to-roman/?tab=Description String M[] = {"", ... 
- lintcode :Integer to Roman 整数转罗马数字
		题目 整数转罗马数字 给定一个整数,将其转换成罗马数字. 返回的结果要求在1-3999的范围内. 样例 4 -> IV 12 -> XII 21 -> XXI 99 -> XC ... 
- [LeetCode] 12. Integer to Roman ☆☆
		Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ... 
随机推荐
- python if not
			判断是否为None的情况 if not x if x is None if not x is None if x is not None`是最好的写法,清晰,不会出现错误,以后坚持使用这种写法. 使用 ... 
- 并发之volatile底层原理
			15.深入分析Volatile的实现原理 14.java多线程编程底层原理剖析以及volatile原理 13.Java中Volatile底层原理与应用 12.Java多线程-java.util.con ... 
- 八皇后(DFS)
			题目描述 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8 ... 
- 涂抹mysql笔记-InnoDB/MyISAM及其它各种存储引擎
			存储引擎:一种设计的存取和处理方式.为不同访问特点的表对象指定不同的存储引擎,可以获取更高的性能和处理数据的灵活性.通常是.so文件,可以用mysql命令加载它. 查看当前mysql数据库支持的存储引 ... 
- 使用jquery+css实现瀑布流布局
			虽然可以直接使用css实现瀑布流布局,但显示的方式有点问题,所以这儿就直接使用jquery+css来实现瀑布流布局,最终效果如下: 思路是通过将每个小块的position设置为relativ ... 
- C语言中的作用域,链接属性和存储类型
			作用域 当变量在程序的某个部分被声明的时候,他只有在程序的一定渔区才能被访问,编译器可以确认4种不同类型的作用域:文件作用域,函数作用域,代码块作用域和原型作用域 1.代码块作用域:位于一对花括号之间 ... 
- oracle 序列sequence
			查询所有的序列: select 'create sequence '||sequence_name|| ' minvalue '||min_value|| ' maxvalue '||max_valu ... 
- rpm梳理
- Jquery中addClass方法不起作用的解决方案
			selected类是要在点击后添加上去的新样式,在点击后,发现没有变化,打开开发者工具,发现selected类已经添加成功了. 在这里没有显示成功的主要原因是后添加的样式表优先级更低,我暂时不清楚具体 ... 
- 6. 添加messager.alert()确定按钮的回调函数,即点完确定按钮后触发的事件
			添加messager.alert()确定按钮的回调函数,即点完确定按钮后触发的事件: $.messager.alert('提示信息', "请联系管理员处理!", 'info', f ... 
