Extensible cpp solution


  • 0
    T
     string symbols ="IVXLCDM";
        string intToRoman(int num) {
            string result = "";
            int sym_s = 0;
            while(num){
                int remain = num % 10;
                num = num / 10;
                string cur = "";
                if(remain > 0 && remain < 4){
                    fill_n(back_inserter(cur), remain, symbols[sym_s]);
                }else if(4 == remain)
                {
                    cur += symbols[sym_s];
                    cur += symbols[sym_s+1];
                }else if(remain > 4 && remain < 9){
                    cur += symbols[sym_s+1];
                    fill_n(back_inserter(cur), remain - 5, symbols[sym_s]);
                }else if(9 == remain){
                    cur += symbols[sym_s];
                    cur += symbols[sym_s+2];
                }
                result = cur + result;
                sym_s = sym_s + 2;
            }
            return result;
        }

Log in to reply
 

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