5.9 ratio
| 6 | |
| 7 | // 5.9 ratio |
| 8 | class Solution { |
| 9 | public: |
| 10 | // straight forward version |
| 11 | void setZeroes1(vector<vector<int>>& matrix) { |
| 12 | int m = matrix.size(); |
| 13 | if (m == 0) return; |
| 14 | int n = matrix[0].size(); |
| 15 | vector<vector<int>> mx = matrix; |
| 16 | for (int i=0; i<m; i++) { |
| 17 | for (int j=0; j<n; j++) { |
| 18 | if (mx[i][j] == 0) { |
| 19 | for (int i=0; i<m; i++) { |
| 20 | matrix[i][j] = 0; |
| 21 | } |
| 22 | for (int j=0; j<n; j++) { |
| 23 | matrix[i][j] = 0; |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | } |
| 30 | |
| 31 | // straight forward version |
| 32 | void setZeroes2(vector<vector<int>>& matrix) { |
| 33 | vector<bool> icz (matrix.size(), false); |
| 34 | if (icz.size() == 0) return; |
| 35 | vector<bool> jcz (matrix[0].size(), false); |
| 36 | for (int i=0; i<matrix.size(); i++) { |
| 37 | for (int j=0; j<matrix[0].size(); j++) { |
| 38 | if ( matrix[i][j] == 0 ) { |
| 39 | icz[i] = true; |
| 40 | jcz[j] = true; |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | for (int i=0; i<matrix.size(); i++) { |
| 45 | for (int j=0; j<matrix[0].size(); j++) { |
| 46 | if ( icz[i] or jcz[j] ) { |
| 47 | matrix[i][j] = 0; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | // constant space |
| 55 | void setZeroes(vector<vector<int>>& matrix) { |
| 56 | bool frow = false; |
| 57 | for (int i=0; i<matrix.size(); i++) { |
| 58 | for (int j=0; j<matrix[0].size(); j++) { |
| 59 | if ( matrix[i][j] == 0 ) { |
| 60 | if (i == 0) frow = true; |
| 61 | else matrix[i][0] = 0; |
| 62 | matrix[0][j] = 0; |
| 63 | } |
| 64 | } |
| 65 | } |
nothing calls this directly
no outgoing calls
no test coverage detected