2018-11-21 组合排列

image.png
class Solution {
    public List letterCombinations(String digits) {
         List res = new ArrayList();
        String s = "";
        char[] chars = digits.toCharArray();
        if(chars==null || chars.length<=0){
            return res;
        }
       
        doLetterCombinations(res,s,chars,0);
        return res;
    }
    
    public void doLetterCombinations(List res,String s,char[] chars,int start){
        if(start == chars.length){
            res.add(s);
            return;
        }
        char[] c = getChars(chars[start]);
        start += 1;
        for(int i = 0;i
image.png
class Solution {
    public List generateParenthesis(int n) {
        List res = new ArrayList();
        StringBuilder sb = new StringBuilder();
        doGenerateParenthesis(res,sb,n,n);
        
        return res;
    }
    
    public void doGenerateParenthesis( List res, StringBuilder sb,int left,int right){
        if(left == 0 && right == 0){
            res.add(sb.toString());
            return;
        }
        if( left < 0 || right <0){
            return;
        }
        
        if(right>left){
            sb.append(')');
            doGenerateParenthesis(res, sb,left,right-1);
            sb.deleteCharAt(sb.length()-1);
        }
        
        sb.append('(');
        doGenerateParenthesis(res, sb,left - 1,right);
        sb.deleteCharAt(sb.length()-1);

    }
}
image.png
class Solution {
    public List> combinationSum(int[] candidates, int target) {
        List> res = new ArrayList();
        List ele = new ArrayList();
        doCombinationSum(res,ele,target,candidates,0);
        return res;
    }
    
    private void doCombinationSum(List> res,  List ele,int target,int[] candidates,int start){
        if(target == 0){
            res.add(new ArrayList(ele));
            return;
        }
        if(target < 0){
            return;
        }
        
        for(int i=start; i
image.png
class Solution {
    public List> combinationSum2(int[] candidates, int target) {
        List> res = new ArrayList();
        List ele = new ArrayList();
        Arrays.sort(candidates);
        docombinationSum2(res,ele,0,target,candidates);
        return res;
    }
    public void docombinationSum2(List res, List ele,int start,int target,int[] candidates){
        if(target == 0){
            res.add(new ArrayList(ele));
            return;
        }
        
        if(target< 0 || start >= candidates.length){
            return;
        }
        
        for(int i = start;istart && candidates[i] == candidates[i-1]) continue;
            ele.add(candidates[i]);
            docombinationSum2(res, ele,i+1,target - candidates[i],candidates);
            ele.remove(ele.size() - 1);
        }
        
        
    }
}
image.png
class Solution {
    public List> combine(int n, int k) {
        List> res = new ArrayList();
        
        List ele = new ArrayList();
        doCombine(res,ele,1,n,k);
        return res;
    }
    public void doCombine(List> res,List ele,int start,int n,int k){
        if(k==0){
            res.add(new ArrayList(ele));
            return;
        }
        if(start>n){
            return;
        }
        
        for(int i = start;i<=n;i++){
            ele.add(i);
            doCombine(res,ele,i+1,n,k-1);
            ele.remove(ele.size()-1);
        }
    }
}
image.png
class Solution {
    public List> permute(int[] nums) {
        List> res = new ArrayList();
        List ele = new ArrayList();
        
        doPerumte(res,ele,nums);
        return res;
    }
    
    public void doPerumte(List> res,List ele ,int[] nums){
        
        if(ele.size() == nums.length){
            res.add(new ArrayList(ele));
            return;
        }
        
        for(int i = 0 ;i < nums.length;i++){
            if(ele.contains(nums[i])){
                continue;
            }
            ele.add(nums[i]);
            doPerumte(res, ele ,nums);
            ele.remove(ele.size()-1);
        }
        
    }
}
image.png
class Solution {
    public List> combinationSum3(int k, int n) {
        List> res = new ArrayList();
        List ele = new ArrayList();
        
        doCombinationSum3(res,ele,k,n,0);
        
        return res;
    }
    public void doCombinationSum3(List> res,List ele,int k,int n,int start){
        
        if(k==0 && n==0){
            res.add(new ArrayList(ele));
            return;
        }
        if(k<0 || n <0){
            return;
        }
        
        for(int i=start;i<9;i++){
            ele.add(i+1);
            doCombinationSum3(res,ele,k-1,n-i-1,i+1);
            ele.remove(ele.size()-1);
        }
        
    }
}
image.png

超时

class Solution {
    public int combinationSum4(int[] nums, int target) {
        return doCombinationSum4(nums,target);
    }
    public int doCombinationSum4(int[] nums,int target){
        if(target == 0){
            return 1;
        }
        if(target < 0){
            return 0;
        }
        int sum = 0;
        for(int i=0;i

你可能感兴趣的:(2018-11-21 组合排列)