題號:73 標題:Set Matrix Zeroes 難度:Medium
Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's, and return the matrix.
You must do it in place.
Example 1:
Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]
Example 2:
Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]
Constraints:
•	m == matrix.length
•	n == matrix[0].length
•	1 <= m, n <= 200
•	-231 <= matrix[i][j] <= 231 - 1
Follow up:
•	A straightforward solution using O(mn) space is probably a bad idea.
•	A simple improvement uses O(m + n) space, but still not the best solution.
•	Could you devise a constant space solution?
我的程式碼
class Solution {
    public void setZeroes(int[][] matrix) {
        int i,j=0,k=0,size=(2*matrix[0].length*matrix.length) ;
        int[] temp = new int[size];
        Arrays.fill(temp, -1);
        for(i=0;i<matrix.length;i++){
            for(j=0;j<matrix[0].length;j++){
                if(matrix[i][j]==0){
                    temp[k] = i;
                    temp[k+1]=j;
                    k=k+2;
                }
            }
        }
        for(i=0;i<size;i=i+2){     
            if(temp[i]<0){
                break;
            }else{
                for(j=0;j<matrix[0].length;j++){
                    matrix[temp[i]][j] = 0;
                }
                for(j=0;j<matrix.length;j++){
                    matrix[j][temp[i+1]] = 0;
                }
            }
        }
    }
}
思考邏輯
本來以為只有0跟1想說先把要改成0的行跟列全部先存2,這樣就不會因為後面還有0出現而影響判斷,但後來發現陣列中的每個值可以是整數的整個範圍,因此作罷。
但偷懶如我,又想到in-place,代表可以宣告一維,於是宣告一個可以儲存mn2大小的一維,掃到0的座標存起來,兩兩一組,最後再把一維陣列中的座標對應的行跟列轉成0就完成拉
day28心得
原本以為我這個宣告一個陣列是做壞事,但我看解答邏輯也差不多嘛,心安理得了!