프로그래밍/LeetCode
[LeetCode][Python3] 240. Search a 2D Matrix II
snoopybox
2019. 2. 9. 23:42
Problem :
https://leetcode.com/problems/search-a-2d-matrix-ii/
My Solution :
class Solution:
def searchMatrix(self, matrix, target):
"""
:type matrix: List[List[int]]
:type target: int
:rtype: bool
"""
if matrix:
row = 0
col = len(matrix[0])-1
while row < len(matrix) and col >= 0:
val = matrix[row][col]
if val == target:
return True
if val > target:
col -= 1
else:
row += 1
return False
Comment :
우측 상단에서 탐색을 시작한다. target보다 크면 왼쪽으로 한칸 옮기고, target보다 작으면 아래쪽으로 한칸 옮긴다.