We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。
Input: [1,1,2,3,3,4,4,8,8] Output: 2
Input: [3,3,7,7,10,11,11] Output: 10
O(log n)
O(1)
The text was updated successfully, but these errors were encountered:
/** * @param {number[]} nums * @return {number} */ var singleNonDuplicate = function(nums) { let left = 0; let right = nums.length - 1; while (left <= right) { const mid = Math.floor((left + right) / 2); if (nums[mid] === nums[mid - 1]) { if ((right - mid) % 2 === 1) { left = mid + 1; } else { right = mid - 2; } } else if (nums[mid] === nums[mid + 1]) { if ((mid - left) % 2 === 1) { right = mid - 1; } else { left = mid + 2; } } else { return nums[mid]; } } };
Sorry, something went wrong.
No branches or pull requests
540. Single Element in a Sorted Array
给定一个只包含整数的有序数组,每个元素都会出现两次,唯有一个数只会出现一次,找出这个数。
Example 1
Example 2
Note
O(log n)
时间复杂度和O(1)
空间复杂度中运行。The text was updated successfully, but these errors were encountered: