Question Link:
https://leetcode.com/problems/search-a-2d-matrix/
Question Description
Given a sorted matrix, find an element
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true
Simulation
Solution 1: Array Traverse
Search start from top right corner (Start). If value larger than current, curY++, else curX—
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
Implementation
Solution 1:
public boolean searchMatrix(int[][] matrix, int target) {int curX = matrix[0].length - 1;int curY = 0;while (curX >= 0 && curY < matrix.length) {if (matrix[curY][curX] == target) {return true;} else if (matrix[curY][curX] > target) {curX--;} else {curY++;}}return false;}
