leetcode

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

subsets-ii.dart (1208B)


      1 //Return all subsets of numbers in the nums list where nums
      2 //contains duplicate values
      3 
      4 //To solve this I used recursion to go through all possible permutations
      5 //of the numbers list. If it was new then I added it's string representation to 
      6 //a set. If it was not the recursion ended with a null value returned.
      7 
      8 //Time: 275ms Beats: 57.14%
      9 //Memory: 143.5MB Beats: 14.29%
     10 class Solution {
     11   List<List<int>> subsetsWithDup(List<int> nums) {
     12       nums.sort();
     13       return allSubsets(nums,[], {});
     14   }
     15   List<List<int>> allSubsets(List<int> nums, List<int> current, Set<String> used){
     16       String curr = current.toString();
     17       if(used.contains(curr)){
     18           return [];
     19       }
     20       used.add(curr);
     21       List<List<int>> return_list = [];
     22       for(int i = 0 ; i < nums.length ; ++i){
     23           List<int> new_nums = nums.sublist(i + 1);
     24           int currint = nums[i];
     25           List<int> new_curr = List.from(current);
     26           new_curr.add(currint);
     27           List<List<int>> output = allSubsets(List.from(new_nums), new_curr, used);
     28           for(List<int> out in output){
     29               return_list.add(out);
     30           }
     31       }
     32       return_list.add(current);
     33       return return_list;
     34   }
     35 }