Integer to Roman 阿拉伯數字轉羅馬數字@LeetCode
阿新 • • 發佈:2018-12-29
思路:
從大到小貪心
package Level2; /** * Integer to Roman * * Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. * */ public class S12 { public static void main(String[] args) { } public String intToRoman(int num) { if(num < 1 || num > 3999){ return "-1"; } int[] aArray = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; String[] rArray = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; String rNumber = ""; for(int i=0; i<aArray.length; i++){ while(num >= aArray[i]){ rNumber += rArray[i]; num -= aArray[i]; } } return rNumber; } }