Simplest Java solution


  • 0
    M
    public class Solution {
        public String intToRoman(int num) {
            String a[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
            int b[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
            StringBuilder sb=new StringBuilder();
            for(int i=0;i<b.length;i++)
            {
                while(num>=b[i])
                {
                    sb.append(a[i]);
                    num-=b[i];
                }
            }
            return sb.toString();
        }
    }

Log in to reply
 

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