C++ My code works on visual studio 2013, but why it doesn't work on LeetCode?


  • -1
    S

    bool search(vector<int>& nums, int target) {
    int s = nums.size();
    if (s == 0) return false;
    if (s == 1) return nums[0] == target;
    int left = 0, right = s - 1;
    while (left<=right){
    if (nums[left] == target || nums[right] == target)
    return true;
    if (right - left == 1)
    return nums[left] == target || nums[right] == target;

    	if (nums[left] == nums[right]){
    		left++;
    		right--;
    	}
    	else if (nums[left]<nums[right]){
    		if (nums[left]>target || nums[right]<target)
    			return false;
    		else {
    			left++;
    			right--;
    		}
    	}
    	else{
    		int mid = (left + right) / 2;
    		if (nums[mid] == target) return true;
    		else if (target<nums[mid]){
    			if (target>nums[left])
    				right = --mid;
    			else
    				return false;
    		}
    		else{
    			if (target > nums[left]){
    				if (nums[mid] < nums[left])
    					right = --mid;
    				else
    					left = ++mid;
    			}
    			else
    				left = ++mid;
    		}
    	}
    }
    	return false;
    

    }


Log in to reply
 

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