You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
What would you like to Propose?
public class ZeroMatrix {
public static void setZeroes(int[][] matrix) {
boolean firstRowHasZero = false;
boolean firstColHasZero = false;
}
Issue details
public class ZeroMatrix {
public static void setZeroes(int[][] matrix) {
boolean firstRowHasZero = false;
boolean firstColHasZero = false;
}
Additional Information
No response
The text was updated successfully, but these errors were encountered: