<BackTracking> permutation 254 47 60

254. Factor Combinations

class Solution {
    public List<List<Integer>> getFactors(int n) {
        List<List<Integer>> res = new ArrayList<>();
        //corner case
        if(n < 4) return res;
        dfs(n, 2, res, new ArrayList<Integer>());
        return res;
    }
    
    private void dfs(int n, int index, List<List<Integer>> res, List<Integer> subset){
        if(n == 1){
            if(subset.size() > 1){
                res.add(new ArrayList<Integer>(subset));
            }
            return;
        }
        //拆解
        for(int i = index; i <= n; i++){
            if(i > n / i) break;
            if(n % i == 0){
                subset.add(i);
                dfs(n / i, i, res, subset);
                subset.remove(subset.size() - 1);    
            }
        }
        subset.add(n);
        dfs(1, n, res, subset);
        subset.remove(subset.size() - 1);
    }
}

47. Permutations II

有重复元素: gren tea

nums[i] == nums[i - 1] && !visited[i - 1] continue. 两数相等直接跳过。 !visited[i - 1] 如果前面的数字没选取过,则不能选后面这个相同的数字

//排列(雨露均沾)
//nums[i] == nums[i - 1] && !visited[i - 1] continue. 两数相等直接跳过
class Solution {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> res = new ArrayList<>();
        //corner case    
        if(nums == null || nums.length == 0) return res;
        Arrays.sort(nums);
        boolean[] visited = new boolean[nums.length];
        for(int i = 0; i < nums.length; i++){
            visited[i] = false;
        }
        dfs(nums, res, new ArrayList<Integer>(), visited);
        return res;
    }
    
    private void dfs(int[] nums, List<List<Integer>> res, List<Integer> subset, boolean[] visited){
        if(subset.size() == nums.length){
            res.add(new ArrayList<Integer>(subset));
            return;
        }
        for(int i = 0; i < nums.length; i++){
            if(visited[i]) continue;
            if(i >= 1 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
            subset.add(nums[i]);
            visited[i] = true;
            dfs(nums, res, subset, visited);
            subset.remove(subset.size() - 1);
            visited[i] = false;
        }
        
    }
}

 60. Permutation Sequence

例: n = 4, k = 14。

k 转化为 第k个数的下标,则k--。 eg: k = 13(只需做一次)

   1.每次第一顺位的数值 :at = k / (n - 1)!。 at = 13 / 3! =  2。

      第二顺位数字的位置: k = k % (n - 1)!。 k = 13 % 3! = 1。 ans = 3。  对于以3为开头的6个数字中,第二顺位数字排在第2位。

  2. at = k / (n - 1)!。 at = 1 / 2! = 0。                                                          k''的排第2,剩下数字的周期是 2!。

    k = k % (n - 1)。 k = 1 % 2! = 1。 ans = 31。

  3.  at = k / 1!。 at = 1 / 1 = 1。

     k = k % 1!。 k = 1 % 1 = 0。 ans = 314。

  4。 at = k / 1。 at = 0。 ans =3142。

class Solution {
    public String getPermutation(int n, int k) {
        List<String> table = new LinkedList<>(map(n));
        if(n == 1) return table.get(0);
        k--;
        String ans = "";
        while(n > 0){
            int kth = kth(n--);
            int at = k / kth;
            k %= kth;
            ans += table.get(at);
            table.remove(at);
        }
        return ans;
    }
    
    public List<String> map(int n){
        String s = "1,2,3,4,5,6,7,8,9";
        List<String> list = Arrays.asList(s.split(","));
        return list;
    }
    
    public int kth(int n){
        int t = 1;
        for(int i = 1; i < n; i++){
            t *= i;
        }
        return t;
    }
}
原文地址:https://www.cnblogs.com/Afei-1123/p/12006286.html