[LeetCode][Python3] 240. Search a 2D Matrix II
2019. 2. 9. 23:42 |
프로그래밍/LeetCode
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보다 작으면 아래쪽으로 한칸 옮긴다.
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 42. Trapping Rain Water (0) | 2019.03.23 |
---|---|
[LeetCode][Python] 341. Flatten Nested List Iterator (0) | 2019.03.22 |
[LeetCode][Python3] 200. Number of Islands (0) | 2019.02.11 |
[LeetCode][Python3] 300. Longest Increasing Subsequence (0) | 2019.02.10 |
[LeetCode][Python3] 103. Binary Tree Zigzag Level Order Traversal (0) | 2019.02.07 |
[LeetCode][Python3] 279. Perfect Squares (0) | 2019.01.31 |
[LeetCode][Python3] 162. Find Peak Element (0) | 2019.01.25 |
[LeetCode][Python3] 328. Odd Even Linked List (0) | 2019.01.25 |
최근에 달린 댓글 최근에 달린 댓글