Skip to content

Commit 60c216e

Browse files
feat: add php solutions to lc problems: No.0002,0003 (doocs#1948)
1 parent 1b891ce commit 60c216e

File tree

6 files changed

+233
-0
lines changed

6 files changed

+233
-0
lines changed

solution/0000-0099/0002.Add Two Numbers/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,58 @@ impl Solution {
432432
}
433433
```
434434

435+
### **PHP**
436+
437+
```php
438+
/**
439+
* Definition for a singly-linked list.
440+
* class ListNode {
441+
* public $val = 0;
442+
* public $next = null;
443+
* function __construct($val = 0, $next = null) {
444+
* $this->val = $val;
445+
* $this->next = $next;
446+
* }
447+
* }
448+
*/
449+
class Solution {
450+
/**
451+
* @param ListNode $l1
452+
* @param ListNode $l2
453+
* @return ListNode
454+
*/
455+
function addTwoNumbers($l1, $l2) {
456+
$dummy = new ListNode(0);
457+
$current = $dummy;
458+
$carry = 0;
459+
460+
while ($l1 !== null || $l2 !== null) {
461+
$x = $l1 !== null ? $l1->val : 0;
462+
$y = $l2 !== null ? $l2->val : 0;
463+
464+
$sum = $x + $y + $carry;
465+
$carry = (int) ($sum / 10);
466+
$current->next = new ListNode($sum % 10);
467+
$current = $current->next;
468+
469+
if ($l1 !== null) {
470+
$l1 = $l1->next;
471+
}
472+
473+
if ($l2 !== null) {
474+
$l2 = $l2->next;
475+
}
476+
}
477+
478+
if ($carry > 0) {
479+
$current->next = new ListNode($carry);
480+
}
481+
482+
return $dummy->next;
483+
}
484+
}
485+
```
486+
435487
### **...**
436488

437489
```

solution/0000-0099/0002.Add Two Numbers/README_EN.md

+52
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,58 @@ impl Solution {
420420
}
421421
```
422422

423+
### **PHP**
424+
425+
```php
426+
/**
427+
* Definition for a singly-linked list.
428+
* class ListNode {
429+
* public $val = 0;
430+
* public $next = null;
431+
* function __construct($val = 0, $next = null) {
432+
* $this->val = $val;
433+
* $this->next = $next;
434+
* }
435+
* }
436+
*/
437+
class Solution {
438+
/**
439+
* @param ListNode $l1
440+
* @param ListNode $l2
441+
* @return ListNode
442+
*/
443+
function addTwoNumbers($l1, $l2) {
444+
$dummy = new ListNode(0);
445+
$current = $dummy;
446+
$carry = 0;
447+
448+
while ($l1 !== null || $l2 !== null) {
449+
$x = $l1 !== null ? $l1->val : 0;
450+
$y = $l2 !== null ? $l2->val : 0;
451+
452+
$sum = $x + $y + $carry;
453+
$carry = (int) ($sum / 10);
454+
$current->next = new ListNode($sum % 10);
455+
$current = $current->next;
456+
457+
if ($l1 !== null) {
458+
$l1 = $l1->next;
459+
}
460+
461+
if ($l2 !== null) {
462+
$l2 = $l2->next;
463+
}
464+
}
465+
466+
if ($carry > 0) {
467+
$current->next = new ListNode($carry);
468+
}
469+
470+
return $dummy->next;
471+
}
472+
}
473+
```
474+
423475
### **...**
424476

425477
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Definition for a singly-linked list.
3+
* class ListNode {
4+
* public $val = 0;
5+
* public $next = null;
6+
* function __construct($val = 0, $next = null) {
7+
* $this->val = $val;
8+
* $this->next = $next;
9+
* }
10+
* }
11+
*/
12+
class Solution {
13+
/**
14+
* @param ListNode $l1
15+
* @param ListNode $l2
16+
* @return ListNode
17+
*/
18+
function addTwoNumbers($l1, $l2) {
19+
$dummy = new ListNode(0);
20+
$current = $dummy;
21+
$carry = 0;
22+
23+
while ($l1 !== null || $l2 !== null) {
24+
$x = $l1 !== null ? $l1->val : 0;
25+
$y = $l2 !== null ? $l2->val : 0;
26+
27+
$sum = $x + $y + $carry;
28+
$carry = (int) ($sum / 10);
29+
$current->next = new ListNode($sum % 10);
30+
$current = $current->next;
31+
32+
if ($l1 !== null) {
33+
$l1 = $l1->next;
34+
}
35+
36+
if ($l2 !== null) {
37+
$l2 = $l2->next;
38+
}
39+
}
40+
41+
if ($carry > 0) {
42+
$current->next = new ListNode($carry);
43+
}
44+
45+
return $dummy->next;
46+
}
47+
}

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,35 @@ impl Solution {
349349
}
350350
```
351351

352+
### **PHP**
353+
354+
```php
355+
class Solution {
356+
/**
357+
* @param String $s
358+
* @return Integer
359+
*/
360+
function lengthOfLongestSubstring($s) {
361+
$max = 0;
362+
for ($i = 0; $i < strlen($s); $i++) {
363+
$chars = [];
364+
$sub = '';
365+
for ($j = $i; $j < strlen($s); $j++) {
366+
if (in_array($s[$j], $chars)) {
367+
break;
368+
}
369+
$sub .= $s[$j];
370+
$chars[] = $s[$j];
371+
}
372+
if (strlen($sub) > $max) {
373+
$max = strlen($sub);
374+
}
375+
}
376+
return $max;
377+
}
378+
}
379+
```
380+
352381
### **...**
353382

354383
```

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

+29
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,35 @@ impl Solution {
339339
}
340340
```
341341

342+
### **PHP**
343+
344+
```php
345+
class Solution {
346+
/**
347+
* @param String $s
348+
* @return Integer
349+
*/
350+
function lengthOfLongestSubstring($s) {
351+
$max = 0;
352+
for ($i = 0; $i < strlen($s); $i++) {
353+
$chars = [];
354+
$sub = '';
355+
for ($j = $i; $j < strlen($s); $j++) {
356+
if (in_array($s[$j], $chars)) {
357+
break;
358+
}
359+
$sub .= $s[$j];
360+
$chars[] = $s[$j];
361+
}
362+
if (strlen($sub) > $max) {
363+
$max = strlen($sub);
364+
}
365+
}
366+
return $max;
367+
}
368+
}
369+
```
370+
342371
### **...**
343372

344373
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
/**
3+
* @param String $s
4+
* @return Integer
5+
*/
6+
function lengthOfLongestSubstring($s) {
7+
$max = 0;
8+
for ($i = 0; $i < strlen($s); $i++) {
9+
$chars = [];
10+
$sub = '';
11+
for ($j = $i; $j < strlen($s); $j++) {
12+
if (in_array($s[$j], $chars)) {
13+
break;
14+
}
15+
$sub .= $s[$j];
16+
$chars[] = $s[$j];
17+
}
18+
if (strlen($sub) > $max) {
19+
$max = strlen($sub);
20+
}
21+
}
22+
return $max;
23+
}
24+
}

0 commit comments

Comments
 (0)