leetcode

Leetcode submissions
git clone git://git.laack.co/leetcode.git
Log | Files | Refs | README

set-matrix-zeroes.dart (1513B)


      1 //Given a matrix if a given index == 0 then all other values
      2 //in the same row and column should be set to zero. The difficulty
      3 //in this arises because that rule only applies for original zeroes
      4 //and the algorithm must be done in place. 
      5 //My solution iterates through the matrix and when an original 0 is found
      6 //the matrix is iterated through the set the appropriate values to 0. 
      7 //Time: 337ms Beats: 60%
      8 //Memory: 152.1MB Beats: 20%
      9 class Solution {
     10   Set<String> zeroed =  {};
     11   void setZeroes(List<List<int>> matrix) {
     12       for(int i = 0 ; i < matrix.length ; ++i){
     13           for(int x = 0 ; x < matrix[i].length ; ++x){
     14               if(matrix[i][x] == 0){
     15                   if(zeroed.contains(i.toString() + " " + x.toString())){
     16                       continue;
     17                   }
     18                   set_zeroes([i,x], matrix);
     19                   zeroed.add(i.toString() + " " + x.toString());
     20               }
     21           }
     22       }
     23   }
     24   void set_zeroes(List<int> position, List<List<int>> matrix){
     25       int posx = position[0];
     26       int posy = position[1];
     27       for(int i = 0 ; i < matrix[0].length ; ++i){
     28           if(matrix[posx][i] == 0){
     29               continue;
     30           }
     31           matrix[posx][i] = 0;
     32           zeroed.add(posx.toString() + " " + i.toString());
     33       }
     34       for(int i = 0 ; i < matrix.length ; ++i){
     35           if(matrix[i][posy] == 0){
     36               continue;
     37           }
     38           matrix[i][posy] = 0;
     39           zeroed.add(i.toString() + " " + posy.toString());
     40       }
     41   }
     42 }