Looks like no one added any tags here yet for you.
Binary Search
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
easiest way is to use 2 pointers startIdx and endIdx, and check middleIdx
Search a 2D matrix
ou are given an m x n
integer matrix matrix
with the following two properties:
Each row is sorted in non-decreasing 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.
You must write a solution in O(log(m * n))
time complexity.
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
think of the matrix as a 1D array, we have left = first item in matrix, right = last item in matrix. Find the middle item and use binary search to find target
draft
draft
draft
draft