All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0074_search_a_2d_matrix.Solution Maven / Gradle / Ivy

There is a newer version: 1.24
Show newest version
package g0001_0100.s0074_search_a_2d_matrix;

// #Medium #Array #Binary_Search #Matrix

public class Solution {
    public boolean searchMatrix(int[][] matrix, int target) {
        int endRow = matrix.length;
        int endCol = matrix[0].length;
        int targetRow = 0;
        boolean result = false;
        for (int i = 0; i < endRow; i++) {
            if (matrix[i][endCol - 1] >= target) {
                targetRow = i;
                break;
            }
        }
        for (int i = 0; i < endCol; i++) {
            if (matrix[targetRow][i] == target) {
                result = true;
                break;
            }
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy