[LeetCode] 240. Search a 2D Matrix II (Python)
python, algorithms, array, binary-search, divide-and-conquer, matrix
Tags: algorithms, array, binary-search, divide-and-conquer, matrix, python
Categories: LeetCode
- μ΄λ° λ¬Έμ κ° λμ€λ©΄ 컬λΌμ λ€λΆν° νμν μμ κ°λ ₯ν μ΄μ μ΄ μμ
Solution
from typing import List
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
# λ€λΆν° νμνλ κ²μ΄ λ ν¨μ¨μ μ
row, col = 0, len(matrix[0]) - 1
while row < len(matrix) and col >= 0:
current = matrix[row][col]
if current == target:
return True
# λ€λΆν° νμνλκΉ λ°λ‘ λ€μ rowλ‘ λμ΄κ° μ μλ κ±°μ
if current < target:
row += 1
continue
# μ°Ύμ κ°λ₯μ±μ΄ μμΌλ μΌμͺ½μΌλ‘ κ°λ©΄μ νμ
if current > target:
col -= 1
return False
Leave a comment