Skip to main content

Convert to Roman Number

The website leetcode has quite a huge collection of difficult coding questions.

As they say, start small. I tried a easy question (medium according to the site). Converting a number to Roman numerals.

We all know that in Roman number system

M - 1000
D - 500
C - 100
L - 50
X - 10
V - 5
I - 1

And
    CD is 400
    CM is 900
    XL is 40
    XC is 90
    IV is 4
    IX is 9

If I look at many examples, it appears that 4 is never IIII, but IV. Similarly 40 is XL. So I think a symbol never appears 4 times, instead you take the next symbol and subtract it (by placing a symbol to its left).

So here is question. And my algorithm was brute force - it was OK, because the question said that the numbers are between 1 to 3999.

What I did was
  1.     Find the number of thousands and place M for each thousand
  2.     Number is number modulo 1000
  3. Find the number of hundreds
  4. If the hundreds is 9  - place a CM for this
  5. If the hundreds is 4 - place a CD for this
  6. If hundred is more than 4 (but not 9), place a D for 500 and hundreds = hundreds - 5
  7. Now for each hundreds place a C
  8. Find the number of tens
  9. If tens is 9 (90) place XC for this
  10. If tens is 4 (40) place XL for this
  11. If neither, but tens is >=5, place a L for 50 and tens = tens - 5
  12. For each tens place a X
  13. Follow the same principle for units with I and V
Here is java code (which is accepted solution :) )



class Solution {
    public String intToRoman(int num) {
        String ans = "";
        int thousands = num/1000;
        num = num%1000;        
        for(int i=0;i<thousands;i++){
            ans+="M";
        }       
        int hundreds = num/100;
        num =num%100;       
        if(hundreds ==9){
            ans+="CM";
            hundreds =0;
        }else if(hundreds==4){
            ans+="CD";
            hundreds = 0;
        }else if(hundreds >=5){
            ans+="D";
            hundreds -=5;
        }
        for(int i =0;i<hundreds;i++){
            ans+="C";
        }
        int tens = num/10;
        num = num%10;         
        if(tens==9){
            ans+="XC";
            tens=0;
        }else if(tens==4){
            ans+="XL";
            tens = 0;
        }else if(tens>=5){
            ans+="L";
            tens= tens-5;
        }
        for(int i = 0;i<tens;i++){
            ans+="X";
        }
        int units = num;
       
        if(units==9){
            ans+="IX";
            units=0;
        }else if(units==4){
            ans+="IV";
            units = 0;
        }else if(units>=5){
            ans+="V";
            units -=5;
        }
        for(int i =0;i<units;i++){
            ans+="I";
        }
        return ans;        
    }
    public static void main(String args[]){
        int num;
        num = Integer.valueOf(args[0]);
        Solution obj = new Solution();
        String roman = obj.intToRoman(num);
        System.out.println("The number"+num+"in Roman is "+roman);
    }
}

Comments