Solution gives garbage value as it doesn't accept long type. It works fine with gcc


  • 0
    S
    vector<int> plusOne(vector<int> &digits) {
        
                int len =  digits.size();
                long x;
                long sum = 0;
                
                for(vector<int>::iterator it = digits.begin(); it != digits.end(); ++it) {
                    x = (*it) * pow(10,len-1);
                    sum += x;
                    len--;
    
                }
                
                digits.clear();
                
                sum++;
                
                while(sum) {
                    
                    x = sum%10;
                    digits.insert(digits.begin(), x);
                    sum = sum/10;
                    
                    
                }
                
                return digits;
                
                
    
        
    }

Log in to reply
 

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