My java solution


  • 0
    A
    public String intToRoman(int num) {
        int[] key =      {1000,900,500, 400,100,  90, 50,  40, 10,   9,  5,   4,  1};
        String[] value = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        String res = ""; 
        int index = 0;
        while(num != 0){
        	if(num - key[index]>=0){
        		res += value[index];
        		num = num - key[index];
        	}else{
        		index++;
        	}
        }
                
    	return res;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.