Leet - 12

Having fun on Leetcode with C++, Java, Python & GO

Problem 12. Integer to Roman

My attempt(s)

Code

C++

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    string intToRoman(int num) {
        string output="";
        int value[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
        string valueS[13] = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        int count=num;
        int pos=0;
        while (count!=0)
        {
            if (count>=value[pos])
            {
                count=count-value[pos];
                output=output+valueS[pos];
            }
            else
                pos++;
        }
        return output;
    }
};

Java

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
class Solution {
    public String intToRoman(int num) {
        String result="";
        while (num>0){
            if (num>=1000){
                result+="M";
                num-=1000;
            }
            
            else if (num>=900){
                result+="CM";
                num-=900;
            }
        
                 
            else if (num>=500){
                result+="D";
                num-=500;
            }
                
            else if (num>=400){
                result+="CD";
                num-=400;
            }
            
            else if (num>=100){
                result+="C";
                num-=100;
            }
            
            else if (num>=90){
                result+="XC";
                num-=90;
            }
                 
            else if (num>=50){
                result+="L";
                num-=50;
            }
            
            else if (num>=40){
                result+="XL";
                num-=40;
            }
                 
            else if (num>=10){
                result+="X";
                num-=10;
            }
                 
            else if (num>=9){
                result+="IX";
                num-=9;
            }
                 
            else if (num>=8){
                result+="VIII";
                num-=8;
            }
                 
            else if (num>=7){
                result+="VII";
                num-=7;
            }
                 
            else if (num>=6){
                result+="VI";
                num-=6;
            }
                 
            else if (num>=5){
                result+="V";
                num-=5;
            }
                 
            else if (num>=4){
                result+="IV";
                num-=4;
            }
                 
            else if (num>=3){
                result+="III";
                num-=3;
            }
                 
            else if (num>=2){
                result+="II";
                num-=2;
            }
                 
            else if (num>=1){
                result+="I";
                num-=1;
            }
        }
                 
                 
        return result;
    }
}

Explanation of idea

Good solution ref.

Code

Analysis

Licensed under CC BY-NC-SA 4.0