leetcode

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

subsets.dart (1021B)


      1 //Given a list of nums return all possible subsets 
      2 //where there are no repeats.
      3 
      4 //To solve this I used it take it leave it approach to ensure
      5 //no two lists will be the same. If you take it then remove the current
      6 //first element and on the way down the stack add on the current nums[0].
      7 //If you leave it then do not add a value on the way down the stack.
      8 
      9 //Time: 261ms Beats: 70%
     10 //Memory: 141.4MB Beats: 20%
     11 
     12 class Solution {
     13   List<List<int>> subsets(List<int> nums) {
     14       if(nums.length == 0){
     15           return [[]];
     16       }
     17       List<int> subl = nums.sublist(1);
     18       List<List<int>> return_take = subsets(subl);
     19       List<List<int>> return_leave = subsets(subl);
     20       List<List<int>> return_list = [];
     21       for(int i = 0 ; i < return_take.length ; ++i){
     22           return_list.add(return_take[i]);
     23           return_list[return_list.length - 1].add(nums[0]);
     24       }
     25       for(int i = 0 ; i < return_leave.length ; ++i){
     26           return_list.add(return_leave[i]);
     27       }
     28       return return_list;
     29   }
     30 }