[LeetCode]Integer to Roman
来源:互联网 发布:org.apache.tools 编辑:程序博客网 时间:2024/06/07 02:24
题目:给定一个数字,求出对应的罗马数字
算法:
从百度百科(http://baike.baidu.com/view/42061.htm?fr=aladdin#4)可以知道罗马数字组成的规则:
1. V 和 X 左边的小数字只能用 I2. L 和 C 左边的小数字只能用 X3. D 和 M 左边的小数字只能用 C4. I,X,C 中任一个连用不可超过3个5. 不能把 V,L,D 作为小数放在大数左边做减法,放在大数右边做加法只能用一个所以,可以从这些规则中归纳如下:
IV:4IX:9XL:40XC:90CD:400CM:900
public class Solution { public static final String[] ROMAN_STRING = new String[]{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX","V", "IV", "I"};public static final int[] ROMAN_VALUE = new int[]{1000, 900, 500, 400, 100,90, 50, 40, 10, 9,5, 4, 1}; public String intToRoman(int num) { String roman = new String(); while (num > 0) { for (int i=0; i<ROMAN_VALUE.length; ++i) { if (num >= ROMAN_VALUE[i]) { roman += ROMAN_STRING[i]; num -= ROMAN_VALUE[i]; break; } } } return roman; }}
3 0
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- Leetcode - "Roman to Integer" & "Integer to Roman"
- LeetCode:Roman to Integer,Integer to Roman
- LeetCode Integer to Roman & Roman to Integer
- leetcode - roman to integer & integer to roman
- LeetCode:Roman to Integer
- LeetCode: Integer to Roman
- LeetCode: Roman to Integer
- LeetCode Integer to Roman
- LeetCode: Integer to Roman
- LeetCode : Roman to Integer
- [Leetcode] Roman to Integer
- [Leetcode] Integer to Roman
- Leetcode: Integer to Roman
- Leetcode: Roman to Integer
- LeetCode Integer to Roman
- 【leetcode】Roman to Integer
- Thinkphp --- 实现递归取出文件以及文件夹
- 缺少streams.h的解决方法
- 默认win7+开启DEP win7 VirtualProtect 3方法 The KMPlayer 3.0.0.1440 WIN7 ASLR+DEP
- Linux 的多线程编程的高效开发经验
- nodejs less coffeescript 手机模拟器 轻量级编辑器 WebMatrix
- [LeetCode]Integer to Roman
- 菜鸟开始步入程序猿行列
- oracle备忘录(一)---基础知识
- B - Symmetric Order(3.3.1)
- Cocos2dx 3.0 提高篇(十一) xml文档的读取与调用
- Python3.X版本中tkFont到tkinter.font的转变
- Java正则表达式
- UIView的autoresizingMask的详细研究
- Java正则表达式