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[] romans = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; int[] values = {1000,900,500,400,100,90,50,40,10,9,5,4,1}; if (num<0||num>3999) { return null; } if (num==0) { return ""; } String result = new String(); int i=0; while (num>0) { while (num>=values[i]){ //按照values的顺做贪心 result += romans[i]; num -= values[i]; } i++; } return result; } }