iT邦幫忙

2023 iThome 鐵人賽

DAY 19
0

啊~
HR消失了~還沒回我信~~
這週該來選面試時間了吧~~你在哪裡呀~~~

Search a 2D Matrix

Q: https://leetcode.com/problems/search-a-2d-matrix/description/

class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int m = matrix.length;
        int n = matrix[0].length;
        int left = 1;
        int right = m * n;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            int newCol = (mid - 1) % n;
            int newRow = (mid - 1) / n;
            if (matrix[newRow][newCol] == target) {
                return true;
            } else if (matrix[newRow][newCol] > target) {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return false;
    }
}

上一篇
09/18
下一篇
09/20
系列文
30天準備google面試30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言