Why my code has different result on leetcode platform and my own PC


  • 0
    B

    My code has Wrong Answer on leetcode platform, shows:

    Input:[0,0,0,1,2,3], 0
    Output:[-1,2]
    Expected:[0,2]
    

    but on my own PC I get the Expected:[0,2].Anybody could help me?

    class Solution {
    public:
        vector<int> searchRange(vector<int>& nums, int target) {
            int size = nums.size();
            vector<int> res;
            if(size == 0) {
                res.push_back(-1);
                res.push_back(-1);
                return res;
            }
    
            int left = 0;
            int right = size - 1;
            int mid;
            while(left <= right) {
                mid = (left + right) / 2;
                if(nums[mid] == target) break;
                if(target < nums[mid]) right = mid -1;
                if(target > nums[mid]) left = mid + 1;
            }
            if(left <= right) {
                int i = mid, j = mid;
                while(nums[i] == target) i--;
                res.push_back(++i);
                while(nums[j] == target) j++;
                res.push_back(--j);
            } else {
                res.push_back(-1);
                res.push_back(-1);
            }
    
            return res;
        }
    };

Log in to reply
 

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