C++ 4ms ugly solution


  • 0
    class Solution {
    public:
        string numberToWords(int num) {
            if(num==0)  return "Zero";
            vector<int> nums(10,0);
            for(int i=0;i<10;i++){
                int temp=pow(10,9-i);
                nums[i]=num/temp;
                num=num%temp;
            }
            string res="";
            switch(nums[0]){
                case 0: break;
                case 1: res+="One Billion";break;
                case 2: res+="Two Billion";break;
                default: break;
            }
            string m=convert(nums[1],nums[2],nums[3]);
            string t=convert(nums[4],nums[5],nums[6]);
            string g=convert(nums[7],nums[8],nums[9]);
            if(nums[1]||nums[2]||nums[3]){
                if(nums[0]) res+=" ";
                res+=m+" Million";   
            }
            if(nums[4]||nums[5]||nums[6]){
                if(nums[1]||nums[2]||nums[3]) res+=" ";
                res+=t+" Thousand";
            }
            if(nums[7]||nums[8]||nums[9]){
                if(nums[0]||nums[1]||nums[2]||nums[3]||nums[4]||nums[5]||nums[6])   res+=" ";
                res+=g;
            }
            return res;
        }
        string convert(int i1,int i2,int i3){
            if(!i1&&!i2&&!i3)   return "";
            string s="";
            switch(i1){
                case 1: s+="One Hundred";break;
                case 2: s+="Two Hundred";break;
                case 3: s+="Three Hundred";break;
                case 4: s+="Four Hundred";break;
                case 5: s+="Five Hundred";break;
                case 6: s+="Six Hundred";break;
                case 7: s+="Seven Hundred";break;
                case 8: s+="Eight Hundred";break;
                case 9: s+="Nine Hundred";break;
                default: break;
            }
            if(i1&&i2)  s+=" ";
            switch(i2){
                case 1: {
                    int p=10*i2+i3;
                    switch(p){
                        case 10: return s+"Ten";
                        case 11: return s+"Eleven";
                        case 12: return s+"Twelve";
                        case 13: return s+"Thirteen";
                        case 14: return s+"Fourteen";
                        case 15: return s+"Fifteen";
                        case 16: return s+"Sixteen";
                        case 17: return s+"Seventeen";
                        case 18: return s+"Eighteen";
                        case 19: return s+"Nineteen";
                        default: return s;
                    }
                }
                case 2: s+="Twenty";break;
                case 3: s+="Thirty";break;
                case 4: s+="Forty";break;
                case 5: s+="Fifty";break;
                case 6: s+="Sixty";break;
                case 7: s+="Seventy";break;
                case 8: s+="Eighty";break;
                case 9: s+="Ninety";break;
                default: break;
            }
            if((i2&&i3)||(i1&&i3))  s+=" ";
            switch(i3){
                case 1: s+="One";break;
                case 2: s+="Two";break;
                case 3: s+="Three";break;
                case 4: s+="Four";break;
                case 5: s+="Five";break;
                case 6: s+="Six";break;
                case 7: s+="Seven";break;
                case 8: s+="Eight";break;
                case 9: s+="Nine";break;
                default: break;
            }
            return s;
        }
        
    };

Log in to reply
 

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