Strange issue. How is this even possible.


  • 0
    L

    Can someone tell me why my code here doesn't work?
    Input:
    3, 7

    Output:
    [[1,5],[2,4],[1,2,4]]

    Expected:
    [[1,2,4]]

    public class Solution {
    	static List<List<Integer>> res = new ArrayList<List<Integer>>();
    	public List<List<Integer>> combinationSum3(int k, int n) {        
        	if (k==0 || n>45) return res;
            helper(new ArrayList<Integer>(), 1, k, n);
            return res;
        }
        
        public static void helper(List<Integer> curr, int start, int k, int n){
        	if (curr.size()==k){
        		if (n==0){
        			res.add(new ArrayList(curr));
        		}
        		return;
        	}
        	for (int i=start; i<=9; i++){  			
        			curr.add(i);
        			helper(curr, i+1, k, n-i);
        			curr.remove(curr.size()-1);   
        	}
        }
    }

Log in to reply
 

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