2. Binary Search Sorted Array
2. Binary Search Sorted Array
请大家耐心等待
关注微信公共账号,获得最新面试题信息及解答
Facebook: http://www.facebook.
com/ninechapter
Weibo:
http://www.weibo.com/ninechapter
Binary Search & Sorted Array
九章算法IT求职面试培训课程 第2章
www.ninechapter.com
Binary Search
Classical Binary Search
Given an sorted integer array - nums, and an
integer - target. Find the any/first/last
position of target in nums, return -1 if target
doesn’t exist.
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
1. Find 5
How we do binary search?
start mid end
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
1. Find 5, mid=4
How we do binary search?
start mid end
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
index 0 1 2 3 4 5 6 7 8
nums 2 3 5 8 13 21 34 55 89
http://www.ninechapter.com/solutions/search-for-a-
range/
Search Insert Position
http://lintcode.com/en/problem/search-insert-
position/
http://www.ninechapter.com/solutions/search-insert-
position/
Search a 2D Matrix
http://www.lintcode.com/en/problem/search-a-2d-
matrix/
http://www.ninechapter.com/solutions/search-a-2d-
matrix/
Search a 2D Matrix II
http://lintcode.com/en/problem/search-a-2d-matrix-
ii/
[ 0, 1, 2, 4]
[ 1, 2, 6, 9]
[ 3, 5, 7,10]
[ 7, 8, 9,11]
Search a 2D Matrix II
Quadrate Search
[ 2, 4 ]
[ 0, 1, 2, 4] [ 0, 1, 2, 4] [ 5, 7 ]
[ 1, 2, 5, 7] find 7 [ 1, 2, 5, 7]
[ 3, 5 ]
[ 3, 5, 7,10] [ 3, 5, 7,10]
[ 7, 8 ]
[ 7, 8, 9,11] [ 7, 8, 9,11]
[ 7, 10]
[ 9, 11]
Search in a 2D Matrix
Search in a 2D Matrix
Search in a 2D Matrix
First Bad Version
http://lintcode.com/en/problem/first-bad-version/
Find Peak Element
http://lintcode.com/en/problem/find-a-peak/
5 minutes break
Search in Rotated Sorted Array
http://lintcode.com/en/problem/search-in-rotated-
sorted-array/
http://www.ninechapter.com/solutions/search-in-
rotated-sorted-array/
Search in Rotated Sorted Array II
http://lintcode.com/en/problem/search-in-rotated-
sorted-array-ii/
http://www.ninechapter.com/solutions/merge-sorted-
array/
Median of Two Sorted Arrays
http://lintcode.com/en/problem/median-of-two-
sorted-arrays/
http://www.ninechapter.com/solutions/median-of-
two-sorted-arrays/
Recover Sorted Array
http://lintcode.com/problem/recover-rotated-
sorted-array/
Rotate String
http://lintcode.com/en/problem/rotate-string/
Reverse Words in a String
http://lintcode.com/en/problem/reverse-words-in-a-
string/
Conclusion
Binary Search
-- Exclude half every time
Sorted Array
-- If array is sorted, try binary search
-- If array is not sorted, try sort it first