Let’s change the question a little bit.
Find all possible combination of numbers using a pre-defined candidate set.
Each number from the candidate set may be used only once in the combination.For example,
Candidate Set = {10, 1, 2, 7, 6, 1, 5}
Target Number = 8One possible output could be:
1+1+6
1+2+5
1+7
2+6
This question is left as an exercise. You should be able to solve this problem with little modification to the previous algorithm.
The UVa Online Judge website has this same problem called Problem 574 – Sum It Up. If you don’t know about the UVa Online Judge, it is a website I highly recommended. It has tons of programming problems (ranging from easy to difficult) to practice. You may also submit your code to their server, which the machine will run your program against their secret input and automatically judge if your solution is correct.
Read full article from Print All Combinations of a Number as a Sum of Candidate Numbers | LeetCode
No comments:
Post a Comment