zoukankan      html  css  js  c++  java
  • LeetCode 46. Permutations

    原题链接在这里:https://leetcode.com/problems/permutations/

    题目:

    Given a collection of distinct numbers, return all possible permutations.

    For example,
    [1,2,3] have the following permutations:
    [1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

    题解:

    For recursion, first figure out what parameters are needed in each state.

    Here it needs nums array, boolean array used to track visited before, item and result.

    From head of array, get the first num not visited before, mark it as visited, add it to item. Then dfs to next state.

    Backtracking needs to revert, remove num from item and mark it not visited.

    Stop condition, item size is same as nums length.  

    Time Complexity: exponential.

    Space: O(nums.length). regardless res.

    AC Java:

     1 public class Solution {
     2     public List<List<Integer>> permute(int[] nums) {
     3         List<List<Integer>> res = new ArrayList<List<Integer>>();
     4         if(nums == null || nums.length == 0){
     5             return res;
     6         }
     7         boolean [] used = new boolean[nums.length];
     8         helper(nums,used,new ArrayList<Integer>(), res);
     9         return res;
    10     }
    11     private void helper(int[] nums, boolean [] used, List<Integer> item, List<List<Integer>> res){
    12         if(item.size() == nums.length){
    13             res.add(new ArrayList<Integer>(item));
    14             return;
    15         }
    16         for(int i = 0; i<nums.length; i++){
    17             if(!used[i]){
    18                 used[i] = true;
    19                 item.add(nums[i]);
    20                 helper(nums,used,item,res);
    21                 item.remove(item.size()-1);
    22                 used[i] = false;
    23             }
    24         }
    25     }
    26 }

    这道题的迭代方法如下:

    Subsets相似,开始时item先加nums[0], 把item加到res里.

    然后每次添加新的nums[i], 首先把res里的每一个item拿出来, 用cur表示.

    在cur的所有可能位置加上新的元素nums[i], 然后把它加载回res里。

    Note: res原有的item不能保留,所以每次扫描res所有item前新建newRes, 添加完新元素nums[i]的item是要加到newRes中去的,所有可能的item都加完后再把newRes赋值回res去。

    Time Complexity: exponential.

    Space: O(n!). n = nums.length.

    AC Java:

     1 public class Solution {
     2     public List<List<Integer>> permute(int[] nums) {
     3         List<List<Integer>> res = new ArrayList<List<Integer>>();
     4         if(nums == null || nums.length == 0){
     5             return res;
     6         }
     7         List<Integer> item = new ArrayList<Integer>();
     8         item.add(nums[0]);
     9         res.add(item);
    10         
    11         for(int i = 1; i<nums.length; i++){
    12             List<List<Integer>> newRes = new ArrayList<List<Integer>>();
    13             for(int j = 0; j<res.size(); j++){
    14                 List<Integer> cur = res.get(j);
    15                 for(int k = 0; k<=cur.size(); k++){
    16                     // 记得这里要做个copy, 不能直接在原来的cur上加
    17                     item = new ArrayList<Integer>(cur);
    18                     item.add(k, nums[i]);
    19                     newRes.add(item);
    20                 }
    21             }
    22             res = newRes;
    23         }
    24         return res;
    25     }
    26 }

    类似CombinationsLetter Tile Possibilities.

    跟上Permutations II.

  • 相关阅读:
    Java判断一个字符是数字或字母
    java数组和字符串相互转换
    java 字符串截取的三种方法
    Templates && Algorithms
    挖坑——未完成题目列表QwQ
    作业_2018.08.25
    BZOJ1008 [HNOI2008]越狱 (快速幂,组合)
    UR #3 核聚变反应强度( gcd )
    A Super Hero
    NOIP2015 pj
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/4842069.html
Copyright © 2011-2022 走看看