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 d6ef9d3

Browse files
feat: add php solution to lc problems: No.0021-0025 (#2370)
1 parent 933e363 commit d6ef9d3

File tree

15 files changed

+654
-0
lines changed

15 files changed

+654
-0
lines changed

‎solution/0000-0099/0021.Merge Two Sorted Lists/README.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,49 @@ var mergeTwoLists = function (list1, list2) {
554554
};
555555
```
556556

557+
```php
558+
# Definition for singly-linked list.
559+
# class ListNode {
560+
# public $val;
561+
# public $next;
562+
# public function __construct($val = 0, $next = null)
563+
# {
564+
# $this->val = $val;
565+
# $this->next = $next;
566+
# }
567+
# }
568+
569+
class Solution {
570+
/**
571+
* @param ListNode $list1
572+
* @param ListNode $list2
573+
* @return ListNode
574+
*/
575+
576+
function mergeTwoLists($list1, $list2) {
577+
$dummy = new ListNode(0);
578+
$current = $dummy;
579+
580+
while ($list1 != null && $list2 != null) {
581+
if ($list1->val <= $list2->val) {
582+
$current->next = $list1;
583+
$list1 = $list1->next;
584+
} else {
585+
$current->next = $list2;
586+
$list2 = $list2->next;
587+
}
588+
$current = $current->next;
589+
}
590+
if ($list1 != null) {
591+
$current->next = $list1;
592+
} elseif ($list2 != null) {
593+
$current->next = $list2;
594+
}
595+
return $dummy->next;
596+
}
597+
}
598+
```
599+
557600
<!-- tabs:end -->
558601

559602
<!-- end -->

‎solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,49 @@ var mergeTwoLists = function (list1, list2) {
554554
};
555555
```
556556

557+
```php
558+
# Definition for singly-linked list.
559+
# class ListNode {
560+
# public $val;
561+
# public $next;
562+
# public function __construct($val = 0, $next = null)
563+
# {
564+
# $this->val = $val;
565+
# $this->next = $next;
566+
# }
567+
# }
568+
569+
class Solution {
570+
/**
571+
* @param ListNode $list1
572+
* @param ListNode $list2
573+
* @return ListNode
574+
*/
575+
576+
function mergeTwoLists($list1, $list2) {
577+
$dummy = new ListNode(0);
578+
$current = $dummy;
579+
580+
while ($list1 != null && $list2 != null) {
581+
if ($list1->val <= $list2->val) {
582+
$current->next = $list1;
583+
$list1 = $list1->next;
584+
} else {
585+
$current->next = $list2;
586+
$list2 = $list2->next;
587+
}
588+
$current = $current->next;
589+
}
590+
if ($list1 != null) {
591+
$current->next = $list1;
592+
} elseif ($list2 != null) {
593+
$current->next = $list2;
594+
}
595+
return $dummy->next;
596+
}
597+
}
598+
```
599+
557600
<!-- tabs:end -->
558601

559602
<!-- end -->
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Definition for singly-linked list.
2+
# class ListNode {
3+
# public $val;
4+
# public $next;
5+
# public function __construct($val = 0, $next = null)
6+
# {
7+
# $this->val = $val;
8+
# $this->next = $next;
9+
# }
10+
# }
11+
12+
class Solution {
13+
/**
14+
* @param ListNode $list1
15+
* @param ListNode $list2
16+
* @return ListNode
17+
*/
18+
19+
function mergeTwoLists($list1, $list2) {
20+
$dummy = new ListNode(0);
21+
$current = $dummy;
22+
23+
while ($list1 != null && $list2 != null) {
24+
if ($list1->val <= $list2->val) {
25+
$current->next = $list1;
26+
$list1 = $list1->next;
27+
} else {
28+
$current->next = $list2;
29+
$list2 = $list2->next;
30+
}
31+
$current = $current->next;
32+
}
33+
if ($list1 != null) {
34+
$current->next = $list1;
35+
} elseif ($list2 != null) {
36+
$current->next = $list2;
37+
}
38+
return $dummy->next;
39+
}
40+
}

‎solution/0000-0099/0022.Generate Parentheses/README.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,34 @@ impl Solution {
238238
}
239239
```
240240

241+
```php
242+
class Solution {
243+
/**
244+
* @param int $n
245+
* @return string[]
246+
*/
247+
248+
function generateParenthesis($n) {
249+
$result = [];
250+
$this->backtrack($result, '', 0, 0, $n);
251+
return $result;
252+
}
253+
254+
function backtrack(&$result, $current, $open, $close, $max) {
255+
if (strlen($current) === $max * 2) {
256+
$result[] = $current;
257+
return;
258+
}
259+
if ($open < $max) {
260+
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
261+
}
262+
if ($close < $open) {
263+
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
264+
}
265+
}
266+
}
267+
```
268+
241269
<!-- tabs:end -->
242270

243271
<!-- end -->

‎solution/0000-0099/0022.Generate Parentheses/README_EN.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,34 @@ impl Solution {
227227
}
228228
```
229229

230+
```php
231+
class Solution {
232+
/**
233+
* @param int $n
234+
* @return string[]
235+
*/
236+
237+
function generateParenthesis($n) {
238+
$result = [];
239+
$this->backtrack($result, '', 0, 0, $n);
240+
return $result;
241+
}
242+
243+
function backtrack(&$result, $current, $open, $close, $max) {
244+
if (strlen($current) === $max * 2) {
245+
$result[] = $current;
246+
return;
247+
}
248+
if ($open < $max) {
249+
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
250+
}
251+
if ($close < $open) {
252+
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
253+
}
254+
}
255+
}
256+
```
257+
230258
<!-- tabs:end -->
231259

232260
<!-- end -->
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
/**
3+
* @param int $n
4+
* @return string[]
5+
*/
6+
7+
function generateParenthesis($n) {
8+
$result = [];
9+
$this->backtrack($result, '', 0, 0, $n);
10+
return $result;
11+
}
12+
13+
function backtrack(&$result, $current, $open, $close, $max) {
14+
if (strlen($current) === $max * 2) {
15+
$result[] = $current;
16+
return;
17+
}
18+
if ($open < $max) {
19+
$this->backtrack($result, $current . '(', $open + 1, $close, $max);
20+
}
21+
if ($close < $open) {
22+
$this->backtrack($result, $current . ')', $open, $close + 1, $max);
23+
}
24+
}
25+
}

‎solution/0000-0099/0023.Merge k Sorted Lists/README.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,64 @@ public class Solution {
343343
}
344344
```
345345

346+
```php
347+
# Definition for singly-linked list.
348+
class ListNode {
349+
public $val;
350+
public $next;
351+
public function __construct($val = 0, $next = null)
352+
{
353+
$this->val = $val;
354+
$this->next = $next;
355+
}
356+
}
357+
358+
class Solution {
359+
/**
360+
* @param ListNode[] $lists
361+
* @return ListNode
362+
*/
363+
364+
function mergeKLists($lists) {
365+
$numLists = count($lists);
366+
367+
if ($numLists === 0) {
368+
return null;
369+
}
370+
while ($numLists > 1) {
371+
$mid = intval($numLists / 2);
372+
for ($i = 0; $i < $mid; $i++) {
373+
$lists[$i] = $this->mergeTwoLists($lists[$i], $lists[$numLists - $i - 1]);
374+
}
375+
$numLists = intval(($numLists + 1) / 2);
376+
}
377+
return $lists[0];
378+
}
379+
380+
function mergeTwoLists($list1, $list2) {
381+
$dummy = new ListNode(0);
382+
$current = $dummy;
383+
384+
while ($list1 != null && $list2 != null) {
385+
if ($list1->val <= $list2->val) {
386+
$current->next = $list1;
387+
$list1 = $list1->next;
388+
} else {
389+
$current->next = $list2;
390+
$list2 = $list2->next;
391+
}
392+
$current = $current->next;
393+
}
394+
if ($list1 != null) {
395+
$current->next = $list1;
396+
} elseif ($list2 != null) {
397+
$current->next = $list2;
398+
}
399+
return $dummy->next;
400+
}
401+
}
402+
```
403+
346404
<!-- tabs:end -->
347405

348406
<!-- end -->

‎solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,64 @@ public class Solution {
342342
}
343343
```
344344

345+
```php
346+
# Definition for singly-linked list.
347+
class ListNode {
348+
public $val;
349+
public $next;
350+
public function __construct($val = 0, $next = null)
351+
{
352+
$this->val = $val;
353+
$this->next = $next;
354+
}
355+
}
356+
357+
class Solution {
358+
/**
359+
* @param ListNode[] $lists
360+
* @return ListNode
361+
*/
362+
363+
function mergeKLists($lists) {
364+
$numLists = count($lists);
365+
366+
if ($numLists === 0) {
367+
return null;
368+
}
369+
while ($numLists > 1) {
370+
$mid = intval($numLists / 2);
371+
for ($i = 0; $i < $mid; $i++) {
372+
$lists[$i] = $this->mergeTwoLists($lists[$i], $lists[$numLists - $i - 1]);
373+
}
374+
$numLists = intval(($numLists + 1) / 2);
375+
}
376+
return $lists[0];
377+
}
378+
379+
function mergeTwoLists($list1, $list2) {
380+
$dummy = new ListNode(0);
381+
$current = $dummy;
382+
383+
while ($list1 != null && $list2 != null) {
384+
if ($list1->val <= $list2->val) {
385+
$current->next = $list1;
386+
$list1 = $list1->next;
387+
} else {
388+
$current->next = $list2;
389+
$list2 = $list2->next;
390+
}
391+
$current = $current->next;
392+
}
393+
if ($list1 != null) {
394+
$current->next = $list1;
395+
} elseif ($list2 != null) {
396+
$current->next = $list2;
397+
}
398+
return $dummy->next;
399+
}
400+
}
401+
```
402+
345403
<!-- tabs:end -->
346404

347405
<!-- end -->

0 commit comments

Comments
(0)

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