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

For example,
If n = 4 and k = 2, a solution is:

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

DFS is the standard way of solving combination problem.

public class Solution {
    /**
    * DFS
    * @runtime: 30ms
    */
    public List<List<Integer>> combine(int n, int k) {
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        dfs(1, n, k, ret, new ArrayList<Integer>());
        return ret;
    }
    
    private void dfs(int begin, int n, int k, List<List<Integer>> ret, List<Integer> path) {
        if (k == 0) {
            ret.add(new ArrayList(path));
            return;
        }
        
        for (int i=begin; i<n+1; i++) {
            path.add(i);
            dfs(i+1, n, k-1, ret, path);
            path.remove(path.size()-1);
        }
    }
}

imgRuntime: 30ms