【java】77. Combinations

原文链接https://leetcode-cn.com/problems/combinations/description/

class Solution {
    List<List<Integer>> res = new LinkedList<>();
	public List<List<Integer>> combine(int n, int k) {
        hel(n,1,k,new LinkedList<>());
		return res;
    }
	public void hel(int n,int pos, int k,List<Integer> list) {
		if(k == 0) {
			List<Integer> tmp = new LinkedList<>(list);
			res.add(tmp);
			return;
		}
		
		for(int i = pos;i <= n-k+1;i++) {
			list.add(i);
			hel(n,i+1,k-1,list);
			list.remove(list.size()-1);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/amber804105/article/details/81265576