C++


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

Log in to reply
 

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