馬老師Python全系列大師課2022
? public boolean divideGroups(int[] nums, int start, int target, int current, int k) { ?
? ? ? ?if (k == 1) return true; // 分組操作執(zhí)行k-1次之后,最后剩余的元素,就是最后一組了,不需要再匹配 ? ? ?
? ? ? ?if (current == target) return divideGroups(nums, nums.length - 1, target, 0, k - 1); // 分組操作執(zhí)行k-1次后,最后剩余的元素,就是最后一組了,不需要再匹配 ? ? ?
? ? ? ?for (int i = start; i >= 0; i--) {
? ? ? ? ? ?if (numUsed[i] == 1 || current + nums[i] > target) continue; // 被使用的元素,不能再次使用;總和大于目標值,也不能使用
? ? ? ? ? ?numUsed[i] = 1; // 標記占用
? ? ? ? ? ?if (divideGroups(nums, i - 1, target, current + nums[i], k)) return true;
? ? ? ? ? ?numUsed[i] = 0; // 撤銷標記
? ? ? ? ? ?while (i > 0 && nums[i - 1] == nums[i]) i--; // 例如“12333333...”,假如最右側(cè)的“3”這個值沒有匹配上,那么它左側(cè)的剩余五個“3”都不需要再匹配了。
? ? ? ?}
? ? ? ?return false;