Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fb39012

Browse files
committed
Add solution 39.
1 parent 1854598 commit fb39012

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /