Easy Understand Java Solution :--D


  • 0
    G
    public static String intToRoman(int num){
    	String result="";
    	int i=0;
    	while(num>=1){
    		int part=num%10;
    		if(part==0){//cases like ”105“ or "1002"
    			i++;
    			num/=10;
    			continue;
    		}
    		part*=Math.pow(10,i++);
    		switch(part){
    		case 1:result="I"+result;break;
    		case 2:result="II"+result;break;
    		case 3:result="III"+result;break;
    		case 4:result="IV"+result;break;
    		case 5:result="V"+result;break;
    		case 6:result="VI"+result;break;
    		case 7:result="VII"+result;break;
    		case 8:result="VIII"+result;break;
    		case 9:result="IX"+result;break;
    		case 10:result="X"+result;break;
    		case 20:result="XX"+result;break;
    		case 30:result="XXX"+result;break;
    		case 40:result="XL"+result;break;
    		case 50:result="L"+result;break;
    		case 60:result="LX"+result;break;
    		case 70:result="LXX"+result;break;
    		case 80:result="LXXX"+result;break;
    		case 90:result="XC"+result;break;
    		case 100:result="C"+result;break;
    		case 200:result="CC"+result;break;
    		case 300:result="CCC"+result;break;
    		case 400:result="CD"+result;break;
    		case 500:result="D"+result;break;
    		case 600:result="DC"+result;break;
    		case 700:result="DCC"+result;break;
    		case 800:result="DCCC"+result;break;
    		case 900:result="CM"+result;break;
    		case 1000:result="M"+result;break;
    		case 2000:result="MM"+result;break;
    		case 3000:result="MMM"+result;break;
    		}
    		num/=10;
    	}
    	return result;
    }

Log in to reply
 

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