Java 6ms beats 90% solution


  • 0
    S
    public class Solution {
    	List<List<Integer>> res;
    	public List<List<Integer>> combine(int n, int k) {
    		res = new ArrayList<List<Integer>>();
    		helper(new ArrayList<Integer>(), 1, n, k);
    		return res;
    	}
    	
    	private void helper(List<Integer> list, int i, int n, int k) {
    		if (i>n-k+1) return;
    		if (k == 0) {res.add(new ArrayList<Integer>(list)); return;}
    		for (int j = i; j <= n; j++) {
    			list.add(j);
    			helper(list, j+1, n, k-1);
    			list.remove(list.size()-1);
    		}
    	}
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.