[LeetCode][JavaScript]Set Matrix Zeroes

Set Matrix Zeroes

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

click to show follow up.

Follow up:

Did you use extra space?
A straight forward 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?

https://leetcode.com/problems/set-matrix-zeroes/

 

 


 

 

数组中如果出现0,把这个数行和列上的数都置成0.

要求常数级的空间复杂度。

一开始想用二进制解决,开2个变量代表行和列。

把行号和列号做2的左移运算,然后用或操作记录,用与判断重复。

大数据越界,挂在倒数3个case上,数组长度有50。

第二种做法,把数组中的0所在的行和列都换成null,最后换回0就好了。

 1 /**
 2  * @param {number[][]} matrix
 3  * @return {void} Do not return anything, modify matrix in-place instead.
 4  */
 5 var setZeroes = function(matrix) {
 6     if(matrix.length > 0){
 7         var rowSize = matrix.length, colSize = matrix[0].length;
 8         var i, j, m, n;
 9         for(i = 0; i < rowSize; i++){
10             for(j = 0; j < colSize; j++){
11                 if(matrix[i][j] === 0){
12                     for(m = 0; m < rowSize; m++){
13                         if(matrix[m][j] !== 0){
14                             matrix[m][j] = null;
15                         }
16                     }
17                     for(n = 0; n < colSize; n++){
18                         if(matrix[i][n] !== 0){
19                             matrix[i][n] = null;
20                         }
21                     }
22                 }                
23             }
24         }
25 
26         for(i = 0; i < rowSize; i++){
27             for(j = 0; j < colSize; j++){
28                 if(matrix[i][j] === null){
29                     matrix[i][j] = 0;
30                 }                
31             }
32         }
33     }
34 };

 

 

 1 /**
 2  * @param {number[][]} matrix
 3  * @return {void} Do not return anything, modify matrix in-place instead.
 4  */
 5 var setZeroes_outOfBundary = function(matrix) {
 6     var row = 0, col = 0;
 7     if(matrix.length > 0){
 8         var rowSize = matrix.length, colSize = matrix[0].length;
 9         var i, j, curr, rowBinary, colBinary;
10         for(i = 0; i < rowSize; i++){
11             for(j = 0; j < colSize; j++){
12                 if(matrix[i][j] === 0){
13                     rowBinary = 2 << i;
14                     colBinary = 2 << j;                    
15                     if((row & rowBinary) === 0){
16                         row = row | rowBinary;
17                     }
18                     if((col & colBinary) === 0){
19                         col = col | colBinary;
20                     }
21                 }                
22             }
23         }
24 
25         for(i = 0; i < rowSize; i++){
26             rowBinary = 2 << i;
27             if((row & rowBinary) !== 0){
28                 for(j = 0; j < colSize; j++){
29                     matrix[i][j] = 0;
30                 }
31             }
32         }
33 
34         for(j = 0; j < colSize; j++){
35             colBinary = 2 << j;
36             if((col & colBinary) !== 0){
37                 for(i = 0; i < rowSize; i++){
38                     matrix[i][j] = 0;
39                 }
40             }
41         }
42     }
43 };

 

 

 

 

 

posted @ 2015-10-05 15:49  `Liok  阅读(360)  评论(0编辑  收藏  举报