-2
\$\begingroup\$

I am making an ArrayList, keeping note of all the indices where zero has occurred. Later on, I am taking out the indices from ArrayList and making the corresponding row and column to zero.

Please suggest improvement and the better ways to do it in place.

     public void setZeroes(int[][] matrix) {
        if(matrix.length == 0 ) {
            return;
        }
        if(matrix.length == 1) {
            boolean flag = false;
            for(int j =0; j<matrix[0].length; j++) {
                if(matrix[0][j] == 0){
                    flag = true;
                    break;
                }
            }
            if(flag) {
                for(int j =0; j<matrix[0].length; j++) {
                    matrix[0][j] = 0;
                    }
                }
            return;
        }
        ArrayList<MyHelperClass> zeroList = new ArrayList<>();
        int m = matrix.length;
        int n = matrix[0].length;
        for(int i =0; i<m; i++) {
            for(int j =0; j<n; j++) {
                if(matrix[i][j] == 0) {
                    MyHelperClass mh = new MyHelperClass(i, j);
                    zeroList.add(mh);
                }
            }
        }

        Iterator ie = zeroList.iterator();
        while(ie.hasNext()) {
            MyHelperClass mh2 = (MyHelperClass) ie.next();
            int x = mh2.x;
            int y = mh2.y;
            for(int j =0; j<n; j++) {
                matrix[x][j] = 0;
                //matrix[i][y] = 0;
            }
            for(int i =0; i<m; i++) {
                matrix[i][y] = 0;
                //matrix[i][y] = 0;
            }
        }


    }

 private static class MyHelperClass {
     int x;
     int y;
     public MyHelperClass(int x, int y){
         this.x = x;
         this.y = y;
     }
 }
\$\endgroup\$
1
  • 4
    \$\begingroup\$ I unfortunately do not have the slightest idea what you intend to do with that code. "Set matrix row column to zero" doesn't explain anything to me. Please include a description of the problem your code is intended to solve \$\endgroup\$
    – Vogel612
    Feb 18, 2016 at 13:19

1 Answer 1

1
\$\begingroup\$

If I get you question right you want to transform a matrix from

1 1 1 1 1
1 1 0 1 1
1 1 1 1 1
1 1 1 1 1

to

1 1 0 1 1
0 0 0 0 0
1 1 0 1 1
1 1 0 1 1

forming a "cross" of zeros for each zero in the original matrix. Your code is pretty hard to read and understand.

Here is how I'd do it:

  1. Iterate over the matrix looking for 0
  2. Remember the x and y coordinates of a 0 in Sets
  3. Iterate over both Sets for x-coordinates and y-coordinates
  4. Setting all values on column x to 0
  5. Setting all values on row y to 0

Here is some code

    private static void replaceInRowAndColumn(int search, int replace, int[][] matrix) {
        RowAndColumnIndexesSets allRowAndColumnIndexes = findAllRowAndColumnIndexesOf(search, matrix);
        for (Integer x : allRowAndColumnIndexes.rows) {
            for (int y = 0; y < matrix[x].length; y++) {
                matrix[x][y] = replace;
            }
        }
        for (int x = 0; x < matrix.length; x++) {
            for (Integer y : allRowAndColumnIndexes.columns) {
                matrix[x][y] = replace;
            }
        }
    }

    private static RowAndColumnIndexesSets findAllRowAndColumnIndexesOf(int search, int[][] matrix) {
        Set<Integer> rowIndexes = new HashSet<Integer>();
        Set<Integer> columnIndexes = new HashSet<Integer>();
        for (int row = 0; row < matrix.length; row++) {
            Set<Integer> allIndexesInCurrentRow = findAllIndexesOf(search, matrix[row]);
            if (!allIndexesInCurrentRow.isEmpty()) {
                columnIndexes.addAll(allIndexesInCurrentRow);
                rowIndexes.add(row);
            }
        }
        return new RowAndColumnIndexesSets(rowIndexes, columnIndexes);
    }

    private static Set<Integer> findAllIndexesOf(int search, int[] row) {
        Set<Integer> indexes = new HashSet<Integer>();
        for (int i = 0; i < row.length; i++) {
            if (row[i] == search) {
                indexes.add(i);
            }
        }
        return indexes;
    }

    private static class RowAndColumnIndexesSets {
        private final Set<Integer> rows;
        private final Set<Integer> columns;

        public RowAndColumnIndexesSets(Set<Integer> rows, Set<Integer> columns) {
            this.rows = rows;
            this.columns = columns;
        }

        public Set<Integer> getRows() {
            return rows;
        }

        public Set<Integer> getColumns() {
            return columns;
        }
    }

Keep in mind that you should try to split you code up into smaller logical (reusable) methods and give them a meaningful name. Also it's not necessary for your problem to remember the coordinate tuple (x,y) of zeros in the matrix. A Set for column indexes and a Set for row indexes is enough. Sets have the advantage that they do not contain duplicate values. If you have a zero at (1,3) and another zero at (2,3) you have to fill zeros in column 3 only once.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.