Submit answer different from local and run?


  • 0
    J

    The submit answer gives me a different answer for test case 124 than running it locally or just the run code button. I thought maybe I was outside of boundaries but I can't find where I would be. Any hint of what I'm doing wrong here would be great.

    1_1484170024714_run-code.png

    0_1484170024713_submit.png

    class Solution {
            public:
                    int threeSumClosest(vector<int>& nums, int target) {
                            sort(nums.begin(), nums.end());
    
                            vit i, j, k, n(nums.end());
                            int curRating(-1), tempVal, retVal;
                            for(i = nums.begin(); i + 2 != n; i++)
                            {
                                    for(j = i+1; j + 1 != n; j++){
                                            k = approxBinarySearch((target-*i-*j), j+1, n);
                                            tempVal = abs(*i+*j+*k-target);
                                            if(curRating == -1 || abs(tempVal) < curRating){
                                                    curRating = abs(tempVal);
                                                    retVal = *i+*j+*k;
                                            }
                                            if(retVal == target){
                                                    return retVal;
                                            }
                                    }
                            }
                            return retVal;
                    }
    
            private:
                    typedef vector<int>::iterator vit;
                    vit approxBinarySearch(int target, vit start, vit end)
                    {
                            if(start == end)
                                    return start;
    
                            vit current = start + distance(start,end)/2; //start in the middle, assuming sorted
                            int direction = *current > target ? -1 : 1;
    
                            if(*current == target || current == start || (current + direction) == end)
                                    return current;
    
    
                            while(current != start && current != end && abs(target - *current) > 0)
                            {
                                    current += direction;
                            }
    
                            if(abs(target - *(current - direction)) < abs(target - *current)){
                                    return (current - direction);
                            } else {
                                    return current;
                            }
                    }
    };
    

  • 0
    J

    LOL, found my answer right after I posted.

    Needed to add check if current was at end of vector :(

                            if(abs(target - *(current - direction)) < abs(target - *current) || current == end){
                                    return (current - direction);
                            } else {
                                    return current;
                            }
    

Log in to reply
 

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