LeetCode第[73]題(Java):Set Matrix Zeroes(矩陣置0)

來源:互聯網
上載者:User

標籤:複雜   str   inpu   ack   blank   leetcode   dia   matrix   不能   

題目:矩陣置0

難度:Easy

題目內容:

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.

翻譯:

給定一個m x n矩陣,如果一個元素是0,就把它的整行和列設為0。

要求:就地置0。

Example 1:

Input: [  [1,1,1],  [1,0,1],  [1,1,1]]Output: [  [1,0,1],  [0,0,0],  [1,0,1]]

Example 2:

Input: [  [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]]


我的思路:因為要求就地,所以不能建立一個矩陣,然後遇見0就將所在行列全部置0

    所以就全部迴圈一遍,將所有的0的行列下標用List<Integer[]>記錄下來,0號位為行,1號位為列。

    然後再迴圈此List,分別調用置零方法即可。

My Code:

 1     public void setZeroes(int[][] matrix) { 2         if (matrix.length == 0 || matrix[0].length == 0) 3             return; 4         List<Integer[]> list = new ArrayList<Integer[]>(); 5         for (int i = 0; i < matrix.length; i++) { 6             for (int j = 0; j < matrix[0].length; j++) { 7                 if (matrix[i][j] == 0) { 8                     list.add(new Integer[]{i,j}); 9                 }10             }11         }12         for (Integer[] x : list) {13             setZero(matrix, x[0], x[1]);14         }15     }16     17     public void setZero(int[][] matrix, int i, int j) {18         for (int n = 0; n < matrix.length; n++) {19             matrix[n][j] = 0;20         }21         for (int m = 0; m < matrix[0].length; m++) {22             matrix[i][m] = 0;23         }24     }

我的複雜度:O(m*n)+ O((m+n)*x)     x為矩陣中0的個數  

編程過程中的問題:

1、無

(代碼中的Integer[]可以使用int[]替換)

 

答案代碼:

 1     public void setZeroes(int[][] matrix) { 2         if (matrix.length == 0 || matrix[0].length == 0) 3             return; 4         boolean row = false,col = false; 5         for (int i = 0; i < matrix.length; i++) { 6             for (int j = 0; j < matrix[0].length; j++) { 7                 if (matrix[i][j] == 0) { 8                     if (i == 0) row = true; 9                     if (j == 0) col = true;10                     matrix[i][0] = 0;11                     matrix[0][j] = 0;12                 }13             }14         }15         for (int i = 1; i < matrix.length; i++) {  // 注意此處應該是從1開始,因為第【0,0】如果為0也進行判斷的話就會把第0列(標誌列)全部設定為0,則接下來整個矩陣都會為016             if (matrix[i][0] == 0) {17                 for (int j = 0; j < matrix[0].length; j++) {18                     matrix[i][j] = 0;19                 }20             }21         }22         for (int j = 1; j < matrix[0].length; j++) {  // 23             if (matrix[0][j] == 0) {24                 for (int i = 0; i < matrix.length; i++) {25                     matrix[i][j] = 0;26                 }27             }28         }29         if (row) {30             for (int j = 0; j < matrix[0].length; j++) {31                 matrix[0][j] = 0;32             }33         }34         if (col) {35             for (int i = 0; i < matrix.length; i++) {36                 matrix[i][0] = 0;37             }38         }39     }

答案複雜度:O(m*n)

答案思路:利用第0行和第0列作為“標誌位”,並設定兩個boolean變數記錄“標誌位”是否需要置0,這種方法相對於我的方法在最後置0的時候避免了重複置0的動作,原矩陣再多的0,最也只需要O(m*n)次置0

LeetCode第[73]題(Java):Set Matrix Zeroes(矩陣置0)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.