You are given an m × n integer matrix matrix where each row is sorted in ascending order from left to right, and each column is sorted in ascending order from top to bottom. Given an integer target, return true if the target exists in the matrix, otherwise return false.
Example 1
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 5
Output: true
Explanation: The element 5 exists in the matrix at row 2, column 2, so the result is true.
Example 2
Input: matrix = [[1,4,7,11,15],[2,5,8,12,19],[3,6,9,16,22],[10,13,14,17,24],[18,21,23,26,30]], target = 20
Output: false
Explanation: The element 20 does not exist in any row or column of the matrix, so the result is false.
Example 3
Input: matrix = [[2,6,10],[5,9,13],[8,12,15]], target = 12
Output: true
Explanation: The element 12 exists in the matrix at row 3, column 2, so the result is true.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
1 4 7 11 15 2 5 8 12 19 3 6 9 16 22 10 13 14 17 24 18 21 23 26 30 5
true
1 4 7 11 15 2 5 8 12 19 3 6 9 16 22 10 13 14 17 24 18 21 23 26 30 20
false