How do I generate integer partitions?

前端 未结 7 1507
悲哀的现实
悲哀的现实 2021-01-20 04:54

I have a list of numbers like 1,2,3 and I want to find all the combination patterns that sum up to a particular number like 5. For example:

Sum=5
Numbers:1,2         


        
相关标签:
7条回答
  • 2021-01-20 05:39

    I would do it recursively starting with the highest numbers first. then, each time in start with the highest level and go in as many levels as numbers. As soon as the cumulative level exceeds your value, drop down to the next number. If still too large (or small), immediately return back one level and decrease THAT to the next number down, then to the next deeper level starting at the top again..

    0 讨论(0)
提交回复
热议问题