15,662,478 members
See more:
I am trying to solve a problem using hashmap.
What I am trying to di is basically store the row no and column no as key and value respectively and when i==row no turn the entire row to 0.
also, when j==column no turn the entire column to 0.

```<pre>import java.util.*;

public class Main {
public static void main(String[] args) {
int[][] matrix={{0,1,2,0},{3,4,5,2},{1,3,1,5}};
setZeroes(matrix);
}

public static void setZeroes(int[][] matrix) {
Map<Integer, Integer> index = new HashMap<>();

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
index.put(i, j);
}
}
}

for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if (index.containsKey(i)) {
matrix[i][j] = 0;
}
if (index.containsValue(j)) {
matrix[i][j] = 0;
}
}
}
System.out.println(Arrays.deepToString(matrix));
}
}```

The problem is in this part
```if (index.containsKey(i)) {
matrix[i][j] = 0;
}
if (index.containsValue(j)) {
matrix[i][j] = 0;
}
```

Could someone please correct my logic?

What I have tried:

Solve the problem on paper and pen and then use the debugger
Posted
0x01AA 13-Nov-22 6:31am
You wrote: ' entire column to 0' or ' entire row to 0'.
But at 'The problem is in this part', you assign only one value. If I understand it correctly you need to assign here either over the rows of a column repectivelly over the columns for a specific row.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Graeme_Grant 30 OriginalGriff 20 Richard MacCutchan 20
 OriginalGriff 2,118 Graeme_Grant 928 Richard Deeming 718 Richard MacCutchan 638 Dave Kreskowiak 473

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900