Skip to content

Latest commit

 

History

History
executable file
·
116 lines (110 loc) · 3.12 KB

77. Combinations.md

File metadata and controls

executable file
·
116 lines (110 loc) · 3.12 KB

77. Combinations

Question

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],
]

Thinking:

  • Method1:回溯
class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        if(n < k) return result;
        int[] arr = new int[n];
        for(int i = 1; i <= n; i++)
            arr[i - 1] = i;
        backtrace(k, 0, arr, new ArrayList<Integer>(), result);
        return result;
    }
    private static void backtrace(int k, int start, int[] arr, List<Integer> list, List<List<Integer>> result){
        if(k == 0) result.add(new ArrayList<Integer>(list));
        else{
            for(int i = start; i <= arr.length - k; i++){
                list.add(arr[i]);
                backtrace(k - 1, i + 1, arr, list, result);
                list.remove(list.size() - 1);
            }
        }
    }
}

二刷

还是通过回溯方法,一开始使用size方法来判断,但是发现其实增加一个参数count进行递归可以大幅加快速度。

class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new LinkedList<>();
        if(k == 0 || n < k) return result;
        backtrace(result, new LinkedList<Integer>(), n, k, 1, 0);
        return result;
    }
    private void backtrace(List<List<Integer>> result, List<Integer> temp, int n, int k, int start, int count){
        if(count == k){
            result.add(new LinkedList<Integer>(temp));
        }else{
            for(int i = start; i <= n; i++){
                temp.add(i);
                backtrace(result, temp, n, k, i + 1, count+1);
                temp.remove(count);
            }
        }
    }
}

Third time

  • Method 1: Need to use comparison to remove redundant recursions.
class Solution {
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> result = new ArrayList<>();
        dfs(result, k, n, new ArrayList<Integer>(), 1, 0);
        return result;
    }
    private void dfs(List<List<Integer>> result, int k, int n, List<Integer> temp, int index, int count){
        if(count == k){
            result.add(new ArrayList<Integer>(temp));
        }else if(count < k && count + (n - index + 1) >= k){
            for(int i = index; i <= n; i++){
                temp.add(i);
                dfs(result, k, n, temp, i + 1, count + 1);
                temp.remove(count);
            }
        }
    }
}

C++ Version

  • Method 1: Recursion
     class Solution {
     	void recursion(int n, int cur, int k, vector<int>& temp, vector<vector<int>>& res){
     		if(k == temp.size()) res.emplace_back(temp);
     		else{
     			for(int i = cur; i <= n; ++i){
     				temp.emplace_back(i);
     				recursion(n, i + 1, k, temp, res);
     				temp.pop_back();
     			}
     		}
     	}
     public:
     	vector<vector<int>> combine(int n, int k) {
     		if(k == 0 || n < k) return {};
     		vector<vector<int>> res;
     		vector<int> cur;
     		recursion(n, 1, k, cur, res);
     		return res;
     	}
     };