Leetcode Binary Search
35. Search Insert Position
Description
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
Click to view full description
Example 1:
- Input:
nums = [1,3,5,6], target = 5 - Output:
2
Example 2:
- Input:
nums = [1,3,5,6], target = 2 - Output:
1
Solution
Idea: 使用二分查找,找到目标值,如果目标值不存在,则返回插入位置。
Complexity: Time: O(log n), Space: O(1)
1 | class Solution: |
74. Search a 2D Matrix
Description
Write an efficient algorithm that searches for a value target in an m x n integer matrix. This matrix has the following properties:
- Each row is sorted in ascending order.
- The first integer of each row is greater than the last integer of the previous row.
Given an integer target, return true if target is in matrix or false otherwise.
Click to view full description
Example 1:
- Input:
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 - Output:
true
Example 2:
- Input:
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 - Output:
false
Solution
Idea: 将二维矩阵看作一维有序数组,使用二分查找,找到目标值。如果目标值不存在,则返回 false。
Complexity: Time: O(log m * n), Space: O(1)
1 | class Solution: |
34. Find First and Last Position of Element in Sorted Array
Description
Given an array of integers nums sorted in non-decreasing order, find the starting and ending positions of a given target value.
If target is not found in the array, return [-1, -1].
Click to view full description
Example 1:
- Input:
nums = [5,7,7,8,8,10], target = 8 - Output:
[3,4]
Example 2:
- Input:
nums = [5,7,7,8,8,10], target = 6 - Output:
[-1,-1]
Solution
Idea: 使用两次二分查找,分别找到目标值的左边界和右边界。
Complexity: Time: O(log n), Space: O(1)
1 | class Solution: |
33. Search in Rotated Sorted Array
Description
There is an integer array nums sorted in ascending order (with distinct values).
Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums.
Click to view full description
Example 1:
- Input:
nums = [4,5,6,7,0,1,2], target = 0 - Output:
4
Example 2:
- Input:
nums = [4,5,6,7,0,1,2], target = 3 - Output:
-1
Solution
Idea: 使用二分查找,找到目标值。
- 找到有序的那一部分:
- 旋转数组总有一部分是有序的
- 如果
nums[left] <= nums[mid],则left到mid是有序的 - 否则,
mid到right是有序的
- 判断
target是否在有序的那一部分:
- 如果
target在有序的那一部分,则继续二分查找 - 否则,
target在另一部分,继续二分查找
Complexity: Time: O(log n), Space: O(1)
1 | class Solution: |
153. Find Minimum in Rotated Sorted Array
Description
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
[4,5,6,7,0,1,2]if it was rotated4times.[0,1,2,4,5,6,7]if it was rotated7times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Given the sorted rotated array nums of unique elements, return the minimum element of this array.
Click to view full description
Example 1:
- Input:
nums = [3,4,5,1,2] - Output:
1
Example 2:
- Input:
nums = [4,5,6,7,0,1,2] - Output:
0
Solution
Idea: 旋转后的数组可以分为两部分: 左侧部分(较大值) 和 右侧部分(较小值)。
- 如果
nums[mid] > nums[right],则最小值在右侧部分,继续二分查找 - 否则,最小值在左侧部分或者就是
nums[mid],继续二分查找
Complexity: Time: O(log n), Space: O(1)
1 | class Solution: |


