题目 :Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
正确代码:package leetcode;
public class newSetMatrixZeroes {
public static void setZeroes(int[][] matrix) {
if (matrix.length < 1 || matrix == null)
return;
int m = matrix.length;
int n = matrix[0].length;
boolean row = false;
boolean col = false;
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) { //先判断,第0行和第0列的元素,如果为0,那么置一个标识,表示这个行(列)为0;
//然后对每一行列找到一个基准:行头(列头)。如果行头为0,那么直接将行置0
if (i == 0)
row = true;
else if (j == 0)
col = true;
else {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i = m - 1; i >= 0; i--) { //为什么是递减的?
for (int j = n - 1; j >= 0; j--) { //因为这里涉及到循环遍历的条件问题:
//如果先开始就把行头或者列头置0,那么这一行(列)就全部为0(因为其以行头为基准!)
//在设置的时候,就从后往前遍历,最后判断行头、列头(不依赖基准)
if (i == 0 && row == true || j == 0 && col == true || matrix[i][0] == 0 || matrix[0][j] == 0)
matrix[i][j] = 0;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = { { 3, 5, 5, 6, 9, 1, 4, 5, 0, 5 }, { 2, 7, 9, 5, 9, 5, 4, 9, 6, 8 },
{ 6, 0, 7, 8, 1, 0, 1, 6, 8, 1 }, { 7, 2, 6, 5, 8, 5, 6, 5, 0, 6 }, { 2, 3, 3, 1, 0, 4, 6, 5, 3, 5 },
{ 5, 9, 7, 3, 8, 8, 5, 1, 4, 3 }, { 2, 4, 7, 9, 9, 8, 4, 7, 3, 7 }, { 3, 5, 2, 8, 8, 2, 2, 4, 9, 8 } };
long time1 = System.currentTimeMillis();
setZeroes(a);
long time = System.currentTimeMillis() - time1;
System.out.println(time + "毫秒!");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
}
我的代码:
package leetcode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class SetMatrixZeroes {
public static void setMatrixZeroes(int[][] a) {
// ArrayList<HashMap> list = new ArrayList<>();
HashMap map = new HashMap<>();
int m = a.length;
int n = a[0].length;
for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++) {
if (a[i][j] == 0) {
map.put(i, j); //为什么不能用hashMap? 因为,同样是第2排的时候,(2,5)会把(2,3)给覆盖掉~!一个key只能对应一个value!
//不能用map存储一个矩阵的位置!!!
}
}
}
/*Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
clear(a, (Integer) entry.getKey(), (Integer) entry.getValue());
* Object key = entry.getKey(); Object val = entry.getValue();
}*/
Iterator it = map.keySet().iterator();
while(it.hasNext()){
int key = (Integer) it.next();
int value = (Integer) map.get(key);
clear(a,key,value);
}
}
public static void clear(int[][] array, int i, int j) {
for (int count = 0; count < array.length; count++) {
array[count][j] = 0;
}
for (int count = 0; count < array[i].length; count++) {
// array[count][j] = 0;
array[i][count] = 0;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] a = { { 3, 5, 5, 6, 9, 1, 4, 5, 0, 5 }, { 2, 7, 9, 5, 9, 5, 4, 9, 6, 8 },
{ 6, 0, 7, 8, 1, 0, 1, 6, 8, 1 }, { 7, 2, 6, 5, 8, 5, 6, 5, 0, 6 }, { 2, 3, 3, 1, 0, 4, 6, 5, 3, 5 },
{ 5, 9, 7, 3, 8, 8, 5, 1, 4, 3 }, { 2, 4, 7, 9, 9, 8, 4, 7, 3, 7 }, { 3, 5, 2, 8, 8, 2, 2, 4, 9, 8 } };
long time1 = System.currentTimeMillis();
setMatrixZeroes(a);
long time = System.currentTimeMillis() - time1;
System.out.println(time + "毫秒!");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
}
注意:写代码,把该写的{}都加上吧~