Advertisement
STANAANDREY

combi sum 4 lc

Apr 21st, 2021
1,153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.50 KB | None | 0 0
  1. class Solution {
  2.     private int []nums;
  3.     public int combinationSum4(int[] nums, int target) {
  4.         this.nums = nums;
  5.         Arrays.sort(this.nums);
  6.         return bktr(target);
  7.     }
  8.     private int bktr(int remaining) {
  9.         if (remaining == 0) {
  10.             return 1;
  11.         }
  12.         int cnt = 0;
  13.         for (int num : nums) {
  14.             if (num > remaining) {
  15.                 break;
  16.             }
  17.             cnt += bktr(remaining - num);
  18.         }
  19.         return cnt;
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement