[td]| # | 名称 | Tag& 频率 | 题目 | 解题 |
| 278 | First Bad Version | BinarySearch
FB高频 | Given n = 5, and version = 4 is the first bad version.
call isBadVersion(3) -> false
call isBadVersion(5) -> true
call isBadVersion(4) -> true
Then 4 is the first bad version. | 二分查找,每次找中间点,如果返回false说明bad version
在右边,true说明在左边。不断缩小范围直到找到第一个。 |
| 35 | Search Insert Position | BinarySearch
GA低频 | Given a sorted array 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.
You may assume no duplicates in the array.
Example 1:
Input: [1,3,5,6], 5
Output: 2 | 这题需要初始化right=n,因为可能插入最后一位后面的index。 |
| 33 | Search in Rotated Sorted Array | BinarySearch
FB,Amazon高频
多公司 | Suppose an array sorted in ascending order is rotated
at some pivot unknown to you beforehand.
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4 | 一个在某个点调转过的排序数组,要求找到给定target的index
二分法具体实现以下三步:
1. 如果mid等于target,返回mid
2. 如果mid>=start, 我们就知道左边是排序的,然后我们看target是否在[start, mid)之间,在的话right=mid,
不再left = mid + 1
3. 如果第2步不成立证明右边是排序的,我们就检测target是否
在(mid, end]之间,在就走右边,不再去左边。
上面第2步,取=号是因为当搜索范围缩小到2个数的时候,
我的mid总会落到左边那个数,这样二分就会往右走。
还要处理一个conor case就是当input只有1个数的时候,
由于我取right = n-1,且循环条件为start<right,
所以只有一个数的时候不会走二分做检查,需要单独写。</right,
|
| | | | |
| 153 | Find Minimum in Rotated Sorted Array | BinarySearch
FLAGMU低频 | Suppose an array sorted in ascending order is rotated
at some pivot unknown to you beforehand.
Find the minimum element.
Input: [4,5,6,7,0,1,2]
Output: 0 | 和33题一样,但是找最小的简单些,
我们只需要拿mid和right比较,如果右边升序,
那最小值肯定在左边end=mid;
不然的话那最小值就在左边right=mid-1
最后left所在index就是最小值。 |
| |
| 162 | Find Peak Element | BinarySearch
FMAG中频 | A peak element is an element that is greater than its neighbors.
Input: nums = [1,2,1,3,5,6,4]
Output: 1 or 5
Explanation: Your function can return either index number 1 where
the peak element is 2, or index number 5 where the peak element is 6. | 查找数组中的peak,可能有多个peak。
因为我们总是拿到中间两个数左边那个,
所以找到mid后我们将他和mid+1比,
如果mid<mid+1,把left=mid+1,不然right=mid |
| 374 | Guess Number Higher or Lower | BinarySearch
Google低频 | We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):
-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
Example :
Input: n = 10, pick = 6
Output: 6 | 从1到n的二分,其他没什么说的有一点就是查找条件加上
等于,可以覆盖只有1个input的情况。 |
| 34 | Find First and last position of element in sorted array | BinarySearch
FLAGU中频 | Given an array of integers nums sorted in ascending order,
find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
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] | 二分法搜索范围的类型题,有多种解法,我们先来看第一种:
最直觉的做法二分找到target然后往两边阔,最坏情况O(n)。
这样显然不符合要求,我们来思考下来看第二种:
做2次二分搜索,一次确定左边界,一次确定右边界,
然后返回结果。二次二分法需要单独写,唯一区别就是
在比较mid和target的时候,当等于的时候不断push另一边:
左边界是当mid>=target,移动right=mid-1
右边界是当mid<=target,移动left=mid+1
第三种做法在面试的时候不容易想到,只需写一个二分法,
我们先找target的左边界,然后找target+1的左边界,
因为target下一个数左边界前一位就是target的右边界。 |
| 349 | Intersection of Two Arrays | BinarySearch
FB, Amazon中频
lyft, uber多公司低频 | Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2] | 最直接暴力解法把两个数组放hashset,然后取出来看在
不再另一个set里,时间复杂度O(n).
第二种解法就是排序数组2然后每次从数组1取一个数
用二分搜索数组2,时间复杂度O(nlogn).
第三者解法排序2个数组然后用two-pointer,也是O(nlogn). |
| 350 | Intersection of Two Arrays II | BinarySearch
FB中频
GAU低频 | Given two arrays, write a function to compute their intersection.
Example 1:
Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2] | follow up不光返回intersection,还要返回重复次数。
我们可以用一个hashmap,key是数字,value是出现次数。
先扫描数组1,把数字和出现次数记录一下,
然后扫描数组2, 如果当前数字在hashmap中且出现次数不为0
则把数加入结果数组,并把相应数字count-- |