My solution in java with 10ms


  • -1
    C

    String[] value = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
    int repeat;
    int count = 0;

        StringBuffer result = new StringBuffer();
        while( num != 0){
            repeat = num/key[count] ;
            for(int i = 1; i <= repeat; i++) {
                result.append(value[count]);
            }
            
            num = num%key[count];
            
            count++;
        } 
        return result.toString();

Log in to reply
 

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