provide alternate logic for subset2 and combination2

This commit is contained in:
Evan Ferrao 2026-02-05 17:54:44 +05:30 committed by GitHub
parent a047db4e15
commit a51e635139
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 1 deletions

View File

@ -14,6 +14,7 @@ class CombinationTwo {
for (int i = currentIndex; i < candidates.length; i++){ for (int i = currentIndex; i < candidates.length; i++){
if (i > currentIndex && candidates[i] == candidates[i-1]) continue; if (i > currentIndex && candidates[i] == candidates[i-1]) continue;
// or i != currentIndex would also work
if (candidates[i] > target) break; if (candidates[i] > target) break;

View File

@ -8,7 +8,8 @@ class SubsetTwo {
ansList.add(new ArrayList<>(ds)); ansList.add(new ArrayList<>(ds));
for (int i = ind; i < nums.length; i++) { for (int i = ind; i < nums.length; i++) {
if (i != ind && nums[i] == nums[i - 1]) continue; if (i > ind && nums[i] == nums[i - 1]) continue;
// or i != ind would also work
ds.add(nums[i]); ds.add(nums[i]);
findSubsets(i + 1, nums, ds, ansList); findSubsets(i + 1, nums, ds, ansList);