코테준비 - Search a 2D Matrix II

정상화·2023년 2월 26일

LeetCode

목록 보기
210/222

Search a 2D Matrix II


class Solution {
private:
    int n;
public:
    bool searchMatrix(vector<vector<int>> &matrix, int target) {
        n = matrix.front().size();

        for (auto &line: matrix) {
            if (binarySearch(line, target, 0, n)) {
                return true;
            }
        }
        return false;
    }

    bool binarySearch(const vector<int> &arr, int target ,int s, int e) {
        int m;
        while (s < e) {
            m = (s + e)/2;
            if (target == arr.at(m)) {
                return true;
            } else if (target < arr.at(m)) {
                e = m;
            } else {
                s = m + 1;
            }
        }
        return false;
    }
};
profile
백엔드 희망

0개의 댓글