|
| 1 | +class Solution { |
| 2 | + |
| 3 | + public function __construct() { |
| 4 | + $this->sol = []; |
| 5 | + $this->nums = null; |
| 6 | + } |
| 7 | + |
| 8 | + /** |
| 9 | + * @param Integer[] $candidates |
| 10 | + * @param Integer $target |
| 11 | + * @return Integer[][] |
| 12 | + */ |
| 13 | + function combinationSum($candidates, $target) { |
| 14 | + sort($candidates); |
| 15 | + $this->nums = $candidates; |
| 16 | + $temp = []; |
| 17 | + self::search($target, $temp, 0); |
| 18 | + return $this->sol; |
| 19 | + } |
| 20 | + |
| 21 | + function search($target, $list, $start){ |
| 22 | + if ($target == 0) { |
| 23 | + array_push($this->sol, $list); |
| 24 | + return; |
| 25 | + } |
| 26 | + for ($i = $start; $i < count($this->nums); $i++){ |
| 27 | + if ($this->nums[$i] > $target) break; |
| 28 | + else { |
| 29 | + array_push($list, $this->nums[$i]); |
| 30 | + self::search($target - $this->nums[$i], $list, $i); |
| 31 | + array_pop($list); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | +} |
0 commit comments