All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0046_permutations.Solution.dart Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Backtracking
// #Algorithm_I_Day_11_Recursion_Backtracking #Level_2_Day_20_Brute_Force/Backtracking
// #Udemy_Backtracking/Recursion #Big_O_Time_O(n*n!)_Space_O(n+n!)
// #2024_10_04_Time_319_ms_(80.00%)_Space_146.8_MB_(92.00%)

class Solution {
  List> permute(List nums) {
    List> ans = [];
    List used = List.filled(nums.length, false);
    helper(ans, [], used, nums);
    return ans;
  }
  
  void helper(List> ans, List temp, List used, List nums) {
    if (temp.length == nums.length) {
      ans.add(List.from(temp));
      return;
    }

    for (int i = 0; i < nums.length; i++) {
      if (used[i]) continue;
      temp.add(nums[i]);
      used[i] = true;
      helper(ans, temp, used, nums);
      temp.removeLast();
      used[i] = false;
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy