GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — LeetCode/L073_Set_Matrix_Zeroes.java at master · likevson/LeetCode Expand file tree/ Copy pathL073_Set_Matrix_Zeroes.javaMore file actions 76 lines (63 loc) · 1.28 KB / Copy pathL073_Set_Matrix_Zeroes.javaFile metadata and controls76 lines (63 loc) · 1.28 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 package LeetCode; public class L073_Set_Matrix_Zeroes { public void setZeroes(int[][] matrix) { if (matrix == null || matrix.length == 0) { return; } int mx = matrix.length; int my = matrix[0].length; // 两个变量,判断第一行和第一列是否有0 boolean xflag = false, yflag = false; // 判断第一行是否有0 for (int i = 0; i < mx; i++) { if (matrix[i][0] == 0) { xflag = true; break; } } // 判断第一列是否有0 for (int i = 0; i < my; i++) { if (matrix[0][i] == 0) { yflag = true; break; } } // 其它行、列是否有0 for (int i = 1; i < mx; i++) { for (int j = 1; j < my; j++) { if (matrix[i][j] == 0) { matrix[i][0] = 0; matrix[0][j] = 0; } } } // 对于第一列,为0,则将所在行变成0 for (int i = 1; i < mx; i++) { if (matrix[i][0] == 0) { for (int j = 0; j < my; j++) { matrix[i][j] = 0; } } } // 对于第一行,为0,则将所在列变成0 for (int i = 0; i < my; i++) { if (matrix[0][i] == 0) { for (int j = 0; j < mx; j++) { matrix[j][i] = 0; } } } // 若原来第一行中有0,则将整行置0 if (xflag) { for (int i = 0; i < mx; i++) { matrix[i][0] = 0; } } // 若原来第一列中有0,则将整列置0 if (yflag) { for (int i = 0; i < my; i++) { matrix[0][i] = 0; } } } } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |