package edu.java.chap1; import java.util.LinkedList; /* * Write an algorithm such that if an element in an M*N matrix is 0, its entire row and column are set to 0. * */ public class SetZero { public static void matrixPrint(int[][] matrix){ System.out.println(); for(int i =0; i < matrix.length;i++){ for(int j = 0; j list = new LinkedList(); outer: for(int i = 0; i list){ for(int j = 0; j < list.size(); j++){ if(i == list.get(j).intValue()){ return true; } } return false; } public static int[][] setZero(int[][] arr){ int[][] result = new int[arr.length][arr[0].length]; //use two boolean[] to store the position, use O(N^2) return result; } public static void main(String[] args) { int[][] arr = {{1,2,3,0},{2,3,0,1},{1,2,3,4},{2,3,4,5},{0,1,2,3}}; matrixPrint(arr); matrixPrint(set0(arr)); } }