Int to Roman Java solution


  • 2
    L

    '''
    public String intToRoman(int num) {

                 // 1-3999                
            String digitsRoman[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
            int digits[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        StringBuilder sb=new StringBuilder();
        
        int i=0;
       while(num>0){
           if(num-digits[i]>=0){
               sb.append(digitsRoman[i]);
               num-=digits[i];
           }else{
               i++;
           }
       }
           return sb.toString();
    }

Log in to reply
 

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