Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE REQUEST] <title> #4877

Closed
Ranci777 opened this issue Oct 22, 2023 · 0 comments
Closed

[FEATURE REQUEST] <title> #4877

Ranci777 opened this issue Oct 22, 2023 · 0 comments

Comments

@Ranci777
Copy link

What would you like to Propose?

public class ZeroMatrix {
public static void setZeroes(int[][] matrix) {
boolean firstRowHasZero = false;
boolean firstColHasZero = false;

    int m = matrix.length;
    int n = matrix[0].length;

    // Check if the first row has any zeros
    for (int j = 0; j < n; j++) {
        if (matrix[0][j] == 0) {
            firstRowHasZero = true;
            break;
        }
    }

    // Check if the first column has any zeros
    for (int i = 0; i < m; i++) {
        if (matrix[i][0] == 0) {
            firstColHasZero = true;
            break;
        }
    }

    // Use first row and first column to mark zeros
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (matrix[i][j] == 0) {
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }
    }

    // Set entire row to zeros
    for (int i = 1; i < m; i++) {
        if (matrix[i][0] == 0) {
            for (int j = 1; j < n; j++) {
                matrix[i][j] = 0;
            }
        }
    }

    // Set entire column to zeros
    for (int j = 1; j < n; j++) {
        if (matrix[0][j] == 0) {
            for (int i = 1; i < m; i++) {
                matrix[i][j] = 0;
            }
        }
    }

    // Handle first row and first column if needed
    if (firstRowHasZero) {
        for (int j = 0; j < n; j++) {
            matrix[0][j] = 0;
        }
    }

    if (firstColHasZero) {
        for (int i = 0; i < m; i++) {
            matrix[i][0] = 0;
        }
    }
}

public static void printMatrix(int[][] matrix) {
    for (int[] row : matrix) {
        for (int col : row) {
            System.out.print(col + " ");
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    int[][] matrix = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};

    System.out.println("Input Matrix:");
    printMatrix(matrix);

    setZeroes(matrix);

    System.out.println("\nOutput Matrix:");
    printMatrix(matrix);
}

}

Issue details

public class ZeroMatrix {
public static void setZeroes(int[][] matrix) {
boolean firstRowHasZero = false;
boolean firstColHasZero = false;

    int m = matrix.length;
    int n = matrix[0].length;

    // Check if the first row has any zeros
    for (int j = 0; j < n; j++) {
        if (matrix[0][j] == 0) {
            firstRowHasZero = true;
            break;
        }
    }

    // Check if the first column has any zeros
    for (int i = 0; i < m; i++) {
        if (matrix[i][0] == 0) {
            firstColHasZero = true;
            break;
        }
    }

    // Use first row and first column to mark zeros
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            if (matrix[i][j] == 0) {
                matrix[i][0] = 0;
                matrix[0][j] = 0;
            }
        }
    }

    // Set entire row to zeros
    for (int i = 1; i < m; i++) {
        if (matrix[i][0] == 0) {
            for (int j = 1; j < n; j++) {
                matrix[i][j] = 0;
            }
        }
    }

    // Set entire column to zeros
    for (int j = 1; j < n; j++) {
        if (matrix[0][j] == 0) {
            for (int i = 1; i < m; i++) {
                matrix[i][j] = 0;
            }
        }
    }

    // Handle first row and first column if needed
    if (firstRowHasZero) {
        for (int j = 0; j < n; j++) {
            matrix[0][j] = 0;
        }
    }

    if (firstColHasZero) {
        for (int i = 0; i < m; i++) {
            matrix[i][0] = 0;
        }
    }
}

public static void printMatrix(int[][] matrix) {
    for (int[] row : matrix) {
        for (int col : row) {
            System.out.print(col + " ");
        }
        System.out.println();
    }
}

public static void main(String[] args) {
    int[][] matrix = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};

    System.out.println("Input Matrix:");
    printMatrix(matrix);

    setZeroes(matrix);

    System.out.println("\nOutput Matrix:");
    printMatrix(matrix);
}

}

Additional Information

No response

@vil02 vil02 closed this as not planned Won't fix, can't repro, duplicate, stale Oct 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants