엔지니어 게시판
LeetCode 솔루션 분류

[7/24] 240. Search a 2D Matrix II

컨텐츠 정보

본문

Medium
7925131Add to ListShare

Write an efficient algorithm that searches for a value target in an m x n integer matrix matrix. This matrix has the following properties:

  • Integers in each row are sorted in ascending from left to right.
  • Integers in each column are sorted in ascending from top to bottom.

 

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

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

 

Constraints:

  • m == matrix.length
  • n == matrix[i].length
  • 1 <= n, m <= 300
  • -109 <= matrix[i][j] <= 109
  • All the integers in each row are sorted in ascending order.
  • All the integers in each column are sorted in ascending order.
  • -109 <= target <= 109
Accepted
633,101
Submissions
1,290,293

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 241 ms, faster than 60.46% of Python3 online submissions for Search a 2D Matrix II.
Memory Usage: 20.5 MB, less than 13.29% of Python3 online submissions for Search a 2D Matrix II.
class Solution:
    def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
        ROW, COL = len(matrix), len(matrix[0])
        
        r,c = 0, COL-1
        
        while r < ROW and c >= 0:
            if matrix[r][c] < target:
                r += 1
            elif matrix[r][c] > target:
                c -= 1
            else:
                return True
        
        return False
전체 404 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 843 명
  • 오늘 방문자 3,684 명
  • 어제 방문자 6,975 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,536 명
알림 0