[leet code] Integer to Roman
来源:互联网 发布:js obj 获取value 编辑:程序博客网 时间:2024/04/30 13:37
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
=========
Analysis:
2 key points:
1. The match between roman numeral and int:
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
In order the deal with the "append left" cases (e.g. 4 = IX, 40 = XL, etc.), we extend the matches as follow: (the most simplest method I could find to deal with these cases)
IV = 4
IX = 9
XL = 40
XC = 90
CD = 400
CM = 900
2. Use StringBuffer to store the result string (dynamic length), and return by .toString() method.
public class Solution { public String intToRoman(int num) { if(num == 0) return null; StringBuffer sb = new StringBuffer(); int numM = num/1000; if(numM > 0){ num -= numM*1000; for (int i=1; i<=numM; i++) sb.append('M'); } int numCM = num/900; if(numCM > 0){ num -= numCM*900; for (int i=1; i<=numCM; i++) sb.append("CM"); } int numD = num/500; if(numD > 0){ num -= numD*500; for (int i=1; i<=numD; i++) sb.append('D'); } int numCD = num/400; if(numCD > 0){ num -= numCD*400; for (int i=1; i<=numCD; i++) sb.append("CD"); } int numC = num/100; if(numC>0){ num -= numC*100; for (int i=1; i<=numC; i++) sb.append('C'); } int numXC = num/90; if(numXC>0){ num -= numXC*90; for (int i=1; i<=numXC; i++) sb.append("XC"); } int numL = num/50; if(numL>0){ num -= numL*50; for (int i=1; i<=numL; i++) sb.append('L'); } int numXL = num/40; if(numXL>0){ num -= numXL*40; for (int i=1; i<=numXL; i++) sb.append("XL"); } int numX = num/10; if(numX>0){ num -= numX*10; for (int i=1; i<=numX; i++) sb.append('X'); } int numIX = num/9; if(numIX>0){ num -= numIX*10; for (int i=1; i<=numIX; i++) sb.append("IX"); } int numV = num/5; if(numV>0){ num -= numV*5; for (int i=1; i<=numV; i++) sb.append('V'); } int numIV = num/4; if(numIV>0){ num -= numIV*4; for (int i=1; i<=numIV; i++) sb.append("IV"); } int numI = num; if(numI>0){ for (int i=1; i<=numI; i++) sb.append('I'); } return sb.toString(); }}
0 0
- [leet code] Roman to Integer
- [leet code] Integer to Roman
- 【LEET-CODE】12. Integer to Roman【Medium】
- Leet Code 12 & 13 Integer <---> Roman
- CODE 118: Roman to Integer
- CODE 119: Integer to Roman
- Leet Code 8 String to Integer (atoi)
- leetcode 日经贴,Cpp code -Roman to Integer
- leetcode 日经贴,Cpp code -Integer to Roman
- [leet code] Reverse Integer
- Leet code---Reverse Integer
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
- Leet Code 7 Reverse Integer
- Integer to Roman & Roman to Integer
- Integer to Roman & Roman to Integer
- LeetCode Roman to Integer && Integer to Roman
- 【leetcode】Roman to Integer | Integer to Roman
- scanf()函数问题
- Google Code Jam Notes - Good Luck - Java
- servlet跳转后页面图片不显示
- Asp.net + JavaScript + Css 笔记
- Median of Two Sorted Arrays
- [leet code] Integer to Roman
- 百炼:(1088)滑雪
- 简单一招用BAT批量获取和修改文件名的方法
- 推荐两款富文本编辑器:NicEdit和Kindeditor
- 从客户端中检测到有潜在危险的 Request.Form 值的详细解决方法
- .net MVC 表单提交
- [leet code] Best Time to Buy and Sell Stock
- 快速排序、三路快排、双基准
- 序列化