(Java)LeetCode-12. 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 Solution {
    public String intToRoman(int num) {
        String ans = "";
		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"};
	    
	    for(int i = 0; i < aArray.length; i++){
	    	while(num>=aArray[i]){
	    		ans += rArray[i];
	    		num -= aArray[i];
	    	}
	    }
	    return ans;
    }
}


你可能感兴趣的:(JAVA,LeetCode)