77. Combinations

别人写的

class
Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> result = new ArrayList<>(); List<Integer> current = new ArrayList<>(); dfs(n, k, result, current, 0, 1); return result; } private void dfs(int n, int k, List<List<Integer>> result, List<Integer> current, int index, int start){ if(index == k){ result.add(new ArrayList<Integer>(current)); return; } for(int i = start; i <= n; i++){ current.add(i); dfs(n, k, result, current, index + 1, i + 1); current.remove(current.size() - 1); } } }

 我写的

class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        if(k > n){
            return result;
        }
        
        int[] options = new int[n + 1];
        for(int i = 1; i <= n; i++){
            options[i] = i;
        }
        // 0 1 2 3 4 
        List<Integer> tmp = new ArrayList<>();
        dfs(result, options, 1, tmp, k);
        return result;
    }
    private void dfs(List<List<Integer>> result, int[] options, int index, List<Integer> tmp, int size){
        if(tmp.size() == size){
            result.add(new ArrayList<>(tmp));
            return;
        }
        
        // add cur number to the tmp
        if(index < options.length){ // boundary , out of bound  without "if" 
            tmp.add(options[index]);
            dfs(result, options, index + 1, tmp, size);
        // tmp is changed , recover
            tmp.remove(tmp.size() - 1);
        
        // not add the current number to tmp
            dfs(result, options, index + 1, tmp, size);
        }
        
    }
}

 

我写的

class
Solution { public List<List<Integer>> combine(int n, int k) { List<List<Integer>> result = new ArrayList<>(); if(k > n) return result; List<Integer> tmp = new ArrayList<>(); dfs(result, tmp, n, k, 1, 0); return result; } private void dfs(List<List<Integer>> result, List<Integer> tmp, int n, int k, int start, int index){ if(index == k){ result.add(new ArrayList<>(tmp)); return; } for(int i = start; i <= n; i++){ tmp.add(i); dfs(result, tmp, n, k, i + 1, index + 1); tmp.remove(tmp.size() - 1); } } }

 

sol 1:
the ending conditon is when size = 2 (k in this case )
we have 4 levels in this case(n = 4)
which is
1
2
3
4

for every level, we have two options, either add current number, or not add current number to the tmp list


sol 2 :

say we have number 1 2 3
and the size of the combination is 2
then the first pos can be 1, 2,3
if we have 1 for pos 1 , then pos 2 can be 2,3,4
if we have 2 for pos 1 , then pos 2 can be 1,3,4
if we have 3 for pos 1 , then pos 2 can be 1,2,4
if we have 4 for pos 1 , then pos 2 can be 1,2,3

we have duplicates , for example 4, 1 and 1,4
so when we implement this, we only pick the element after including and after current index

time: ?


idea: from n intergers (1,2,3,....,n) choose k elements , there is no duplicates in the array from 1 to n
return all unique combinations.

say we have an array int[]{1,2,3,4, 5}
[1,3] is the same as [3, 1]

so here comes the starting index,
so if we choose 1 for pos 1, then for pos 2, our choices are 2, 3,4,5, (the starting index for pos 2 is 1's index + 1)
if we choose 2 for pos 1, then for pos 2 , our choices are 3 4 5 (the starting index for pos 2 is 2's index + 1)

=========================
if we are given an array of integers with duplicate numbers 1,2,3,4,5,1
if we don't sort the array, we can have [1,4] and [4,1]

[1,4] is the same as [4,1] in the combination sense 

  

 

because in combination ,[1,4] is the same as [4,1], so we want to sort the array first and

1,1,2,3,4,5

 

after sorting the array, array is 1, 1 , 2 ,3 , 4 , 5

for pos 1, we choose 1,2,3,4,5, skip the duplicate 1, this is the same as in permutation

in permutation with duplicatrs, we also need to sort the array, so duplicates are close together, and for the same pos, we choose only one of the 

duplciates elements, and skip all other duplicates elements for the current pos. 

 

 

 

the rest of procedure is the same as the regular combinarion., which we use a start index to prevent other type of duplicates

 

 


difference from permuatation is that permutation takes all numbers,  and [1,4] is not the same as [4,1]

and the start index in the for loop is 0.

 

 




Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.

Example:

Input: n = 4, k = 2
Output:
[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

posted on 2018-07-18 08:01  猪猪&#128055;  阅读(118)  评论(0)    收藏  举报

导航