This repository contains implementations of the combinationSum2
algorithm in multiple programming languages: C++, Java, JavaScript, Python, and Go. The algorithm finds all unique combinations of numbers from a list that sum up to a given target. Each combination is unique, and the same number cannot be used multiple times.
-
Sorting the Input Array:
The candidates array is sorted to handle duplicates easily and optimize the backtracking process. This ensures that the algorithm can skip over duplicate elements and stop early when it’s clear that no further combination will meet the target. -
Backtracking Setup:
Backtracking is used to explore all possible combinations. A helper function is defined that recursively tries to build combinations by adding elements from the sorted array. -
Base Case - Target Reached:
If the target becomes zero, it indicates that the current combination sums up to the desired target. This combination is then added to the result list. -
Skipping Duplicates:
To avoid generating duplicate combinations, the algorithm skips over elements that are the same as the previous one when iterating through the array. -
Early Stopping:
If an element in the sorted array exceeds the current target, the loop breaks because any subsequent elements will also be too large. -
Backtracking:
After exploring a combination, the last element added is removed (backtracking) to try other possible combinations.
-
Sort the Input Array:
The candidates array is sorted to make duplicate handling easier. -
Initialize Result and Current Combination Vectors:
Two vectors are initialized, one for storing valid combinations and one for tracking the current combination being explored. -
Backtracking Function:
The main function calls a helper functionbacktrack
, passing the current index, target, current combination, and result. -
Base Case - Target Reached:
If the target is zero, the current combination is added to the result. -
Iterate Through Candidates:
The loop iterates through the candidates, skipping duplicates and stopping early if the candidate exceeds the target. -
Recursively Backtrack:
Thebacktrack
function is called recursively with the updated target and the next index. -
Backtrack - Remove Last Element:
After exploring a path, the last element is removed to explore other combinations.
-
Sort the Array:
The array is sorted for easier handling of duplicates. -
Initialize Result and Temporary List:
Two lists are created: one for storing results and one for the current combination. -
Backtracking Method:
A helper methodbacktrack
is defined to handle the recursive exploration of combinations. -
Base Case - Target Zero:
If the target is zero, a copy of the current list is added to the result. -
Loop Through Candidates:
The loop checks for duplicates and whether the current candidate can be part of a valid combination. -
Recursive Exploration:
Thebacktrack
method is called with an updated target and index. -
Remove Last Element:
Backtrack by removing the last added element before exploring the next candidate.
-
Sort the Candidates Array:
The array is sorted to manage duplicates and allow early stopping. -
Initialize Result and Current Arrays:
Arrays are initialized to store results and the current combination. -
Define Backtracking Function:
A functionbacktrack
is defined to explore combinations. -
Check for Target Zero:
When the target is zero, the current combination is added to the result. -
Iterate Through Candidates:
The loop skips duplicates and breaks early if a candidate is too large. -
Recursive Backtracking:
Callbacktrack
recursively with the remaining target and next index. -
Backtrack by Popping Last Element:
Remove the last element to try other combinations.
-
Sort the Candidates List:
The list is sorted to facilitate duplicate handling and early stopping. -
Initialize Result List:
A list is created to store the valid combinations. -
Backtracking Function:
A nested functionbacktrack
is defined to handle recursive exploration. -
Check for Target Zero:
When the target reaches zero, the current combination is added to the result. -
Loop Through Candidates:
The loop skips duplicates and stops early if a candidate is greater than the target. -
Recursively Call Backtrack:
Callbacktrack
recursively with the reduced target and updated index. -
Backtrack by Removing Last Element:
After exploring a combination, the last element is removed to try another path.
-
Sort the Input Slice:
The slice is sorted for managing duplicates and stopping early. -
Initialize Result and Current Slices:
Slices are initialized to store results and the current combination. -
Define Backtracking Function:
A functionbacktrack
is defined to recursively explore combinations. -
Check for Target Zero:
When the target is zero, the current combination is added to the result. -
Iterate Over Candidates:
The loop checks for duplicates and breaks if a candidate exceeds the target. -
Recursive Backtracking:
Thebacktrack
function is called with the updated target and index. -
Backtrack by Popping Last Element:
The last added element is removed to explore other possibilities.