The MOST EFFICIENT WAY implemented in C++


  • -1
    A

    class Solution {

    public:

    string intToRoman(int num)
    

    {

        int single=num%10;
        int ten=num%100/10;
        int hundred=num%1000/100;
        int thousand=num/1000;
        string res;
        char* single1[9]={"I","II","III","IV","V","VI","VII","VIII","IX"}; //0-9
        char* ten1[9]={"X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        char* hundred1[9]={"C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
        char* thousand1[3]={"M","MM","MMM"};
        if(thousand>0)
            res+=thousand1[thousand-1];
        if(hundred>0)
            res+=hundred1[hundred-1];
        if(ten>0)
            res+=ten1[ten-1];
        if(single>0)
            res+=single1[single-1];
        return res;
    }
    

    };enter code here

    THE most efficient way is enum.


  • 1

    The MOST EFFICIENT WAY

    You're claiming this based on what, exactly?


  • -1
    A

    cause my time cost is O(1)


  • 0

    So is everybody else's.


  • -1
    A

    We can have time comparison


Log in to reply
 

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