LeetCode 46. Permutations47. Permutations II&&131. Palindrome Partitioning(全排列问题)

LeetCode 46. 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],
  [3,2,1]
public class Permutations {
    public List<List<Integer>> permute(int[] nums) {
        List<List<Integer>> list = new ArrayList<>() ;
        //Arrays.sort(nums);
        List<Integer> temp = new ArrayList<>();
        backtrack(list,temp,nums);
        return list;

    }

    private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums){
        if(temp.size()==nums.length) {
            list.add(new ArrayList<>(temp));
        }else {
            for(int i=0;i<nums.length;i++){
                if(temp.contains(nums[i])) continue;
                temp.add(nums[i]);
                backtrack(list,temp,nums);
                temp.remove(temp.size()-1);
            }
        }

    }
}


47. Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,[1,1,2] have the following unique permutations:

[
  [1,1,2],
  [1,2,1],
  [2,1,1]
]


 
public class PermutationsII {
    public List<List<Integer>> permuteUnique(int[] nums) {
        List<List<Integer>> list = new ArrayList<>() ;
        Arrays.sort(nums);
        List<Integer> temp = new ArrayList<>();
        backtrack(list,temp,nums,new boolean[nums.length]);
        return list;

    }

    private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums,boolean[] used){
        if(temp.size()==nums.length) {
            list.add(new ArrayList<>(temp));
        }else {
            for(int i=0;i<nums.length;i++){
                //if(temp.contains(nums[i]) || nums[i]==nums[i+1]) continue;
                if(used[i] || i > 0 && nums[i] == nums[i-1] && !used[i - 1]) continue;
                used[i]=true;
                temp.add(nums[i]);
                backtrack(list,temp,nums,used);
                used[i]=false;
                temp.remove(temp.size()-1);
            }
        }

    }
}


131. Palindrome Partitioning

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s = "aab",
Return

[
  ["aa","b"],
  ["a","a","b"]
]

public class PalindromePartitioning {
    List<List<String>> res;
    List<String> cur;

    public List<List<String>> partition(String s) {
        res=new ArrayList<List<String>>();
        cur = new ArrayList<String>();
        backTrack(s,0);
        return res;
    }

    public void backTrack(String a,int l){
        if(cur.size()>0&& l>=a.length())
           res.add(new ArrayList<>(cur));

        for(int i=l;i<a.length();i++){
            if(isPalindrome(a,l,i)){
                if(i==l){
                    cur.add(Character.toString(a.charAt(i)));
                }else{
                    cur.add(a.substring(l,i+1));
                }
                backTrack(a,i+1);
                cur.remove(cur.size()-1);
            }

        }

    }

    public boolean isPalindrome(String str, int l, int r){
        if(l==r) return true;
        while(l<r){
            if(str.charAt(l)!=str.charAt(r)) return false;
            l++;r--;
        }
        return true;
    }
}


78. Subsets

Given a set of distinct integers, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,3], a solution is:

[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

public class Subsets {
    public List<List<Integer>> subsets(int[] nums) {
        List<List<Integer>> list = new ArrayList<>() ;
        List<Integer> temp = new ArrayList<>();
        backtrack(list,temp,nums,0);
        return list;

    }

    private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums,int start){
        list.add(new ArrayList<>(temp));
        for(int i=start;i<nums.length;i++){
            temp.add(nums[i]);
            backtrack(list,temp,nums,i+1);
            temp.remove(temp.size()-1);
        }
}
}

90. Subsets II

Given a collection of integers that might contain duplicates, nums, return all possible subsets.

Note: The solution set must not contain duplicate subsets.

For example,
If nums = [1,2,2], a solution is:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]

public class SubsetsII {
    public List<List<Integer>> subsetsWithDup(int[] nums) {
        List<List<Integer>> list = new ArrayList<>() ;
        Arrays.sort(nums);
        List<Integer> temp = new ArrayList<>();
        backtrack(list,temp,nums,0);
        return list;

    }

    private void backtrack(List<List<Integer>>list,List<Integer> temp,int[]nums,int start){
        list.add(new ArrayList<>(temp));
        for(int i=start;i<nums.length;i++){
            if(i > start && nums[i] == nums[i-1]) continue;
            temp.add(nums[i]);
            backtrack(list,temp,nums,i+1);
            temp.remove(temp.size()-1);
        }
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值