Does the "sorted" can make any contribution?


  • -1
    Z

    class Solution {
    public:
    int singleNonDuplicate(vector<int>& nums) {
    int left=0,right=nums.size()-1,mid;
    while(left<=right){
    mid=(left+right)>>1;
    if((mid>0 && nums[mid]==nums[mid-1])){
    if(mid%2==0) right=mid-2;
    else left=mid+1;
    }
    else if(mid<nums.size()-1 && nums[mid]==nums[mid+1]){
    if(mid%2==0) left=mid+2;
    else right=mid-1;
    }
    else break;
    }
    return nums[mid];
    }
    };


Log in to reply
 

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