why have to use long? w/solution


  • 0
    2
    class Solution {
    public:
        string fractionToDecimal(int numerator, int denominator) {
            string result="";
            bool p=false;
            long a=labs(numerator),b=labs(denominator);
            long r=a%b;
            
            if(numerator<0^denominator<0 && numerator) result+='-';
            result+=to_string(a/b);
            
            unordered_map<long,int> mp;
            while(1){
                r*=10;
                if(r!=0 && !p) {
                    result+='.';
                    p=true;
                }
                
                
                if(r==0) break;
                else {
                    if(mp.find(r)==mp.end()) mp[r]=result.length();
                    else {
                        result.insert(mp[r],"(");
                        result+=')';
                        break;
                    }
                }
                result+=to_string(r/b);
                r=r%b;
            }
            
            return result;
        }
    };
    

Log in to reply
 

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