Leetcode - 74. Search a 2D Matrix

숲사람·2022년 8월 11일
0

멘타트 훈련

목록 보기
121/237

문제

주어진 2차원배열에서 target값이 존재하는지 확인하라. 각 row/column은 오름차순 정렬되어있다.

해결 O(n)

bool searchMatrix(int** matrix, int matrixSize, int* matrixColSize, int target){
    int row = matrixSize - 1;
    int col = 0;
    while (row >= 0 && row < matrixSize && col >= 0 && col < *matrixColSize) {
        if (target == matrix[row][col])
            return true;
        else if (target < matrix[row][col]) {
            row--;
        } else {
            col++;
        }
    }
    return false;
}
profile
기록 & 정리 아카이브 용도 (보다 완성된 글은 http://soopsaram.com/documentudy)

0개의 댓글