Simple Java Solution


  • 0
    Z
    import java.util.ArrayList;
    import java.util.List;
    
    public class Solution {
        private List<List<Integer>> result = new ArrayList<>();
    
        public List<List<Integer>> combinationSum3(int k, int n) {
            if (k <= 0 || n <= 0) {
                return result;
            }
            combinationSum3(1, k, n, new ArrayList<>());
            return result;
        }
    
        public void combinationSum3(int start, int k, int n, List<Integer> list) {
            if (k == 0 && n == 0) {
                result.add(new ArrayList<>(list));
                return;
            }
            if(k == 0 || n < 0){
                return;
            }
            for (int i = start; i <= 9; i++) {
                list.add(i);
                combinationSum3(i + 1, k - 1, n - i, list);
                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.