A simple java solution


  • 0
    O
    public class Solution {
        public String intToRoman(int num) {
            int numCopy = num;
            String result = "";
            while(numCopy > 0){
                if(numCopy >= 1000){
                    result+="M";
                    numCopy -=1000;
                }
                else if(numCopy >=900){
                    result+="CM";
                    numCopy -=900;                
                }else if(numCopy>=500){
                    result+="D";
                    numCopy -=500;
                }
                else if(numCopy >=400){
                    result+="CD";
                    numCopy -= 400;
                }
                else if(numCopy>=100){
                    result+="C";
                    numCopy-=100;
                }
                else if( numCopy >= 90){
                    result+="XC";
                    numCopy-=90;                
                }
                else if( numCopy >= 50){
                    result +="L";
                    numCopy -=50;
                }
                else if( numCopy >= 40){
                    result +="XL";
                    numCopy-=40;
                }
                else if( numCopy >= 10){
                    result +="X";
                    numCopy-=10;
                }
                else if( numCopy ==9){
                    result+="IX";
                    numCopy-=9;
                }
                else if( numCopy >= 5){
                    result+="V";
                    numCopy -=5;
                }
                else if( numCopy >=4){
                    result+="IV";
                    numCopy -=4;
                }
                else if(numCopy>=1){
                    result+="I";
                    numCopy -=1;
                }
            }
            return result;
        }
    }

Log in to reply
 

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