Skip to content

feat:add php solution to lc problems:No.0004-0010 #2240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add php solution to lc problems:No.0002,0003
  • Loading branch information
ZylalMinollari committed Nov 9, 2023
commit 9c8eb683a74eb345d1074262c5cb9900f7e47ad7
53 changes: 53 additions & 0 deletions solution/0000-0099/0002.Add Two Numbers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,59 @@ impl Solution {
}
```

### **PHP**

```php
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {

/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
function addTwoNumbers($l1, $l2) {
$dummy = new ListNode(0);
$current = $dummy;
$carry = 0;

while ($l1 !== null || $l2 !== null) {
$x = ($l1 !== null) ? $l1->val : 0;
$y = ($l2 !== null) ? $l2->val : 0;

$sum = $x + $y + $carry;
$carry = (int) ($sum / 10);
$current->next = new ListNode($sum % 10);
$current = $current->next;

if ($l1 !== null) {
$l1 = $l1->next;
}

if ($l2 !== null) {
$l2 = $l2->next;
}
}

if ($carry > 0) {
$current->next = new ListNode($carry);
}

return $dummy->next;
}
}
```

### **...**

```
Expand Down
53 changes: 53 additions & 0 deletions solution/0000-0099/0002.Add Two Numbers/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,59 @@ impl Solution {
}
```

### **PHP**

```php
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {

/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
function addTwoNumbers($l1, $l2) {
$dummy = new ListNode(0);
$current = $dummy;
$carry = 0;

while ($l1 !== null || $l2 !== null) {
$x = ($l1 !== null) ? $l1->val : 0;
$y = ($l2 !== null) ? $l2->val : 0;

$sum = $x + $y + $carry;
$carry = (int) ($sum / 10);
$current->next = new ListNode($sum % 10);
$current = $current->next;

if ($l1 !== null) {
$l1 = $l1->next;
}

if ($l2 !== null) {
$l2 = $l2->next;
}
}

if ($carry > 0) {
$current->next = new ListNode($carry);
}

return $dummy->next;
}
}
```

### **...**

```
Expand Down
49 changes: 49 additions & 0 deletions solution/0000-0099/0002.Add Two Numbers/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {

/**
* @param ListNode $l1
* @param ListNode $l2
* @return ListNode
*/
function addTwoNumbers($l1, $l2) {
$dummy = new ListNode(0);
$current = $dummy;
$carry = 0;

while ($l1 !== null || $l2 !== null) {
$x = ($l1 !== null) ? $l1->val : 0;
$y = ($l2 !== null) ? $l2->val : 0;

$sum = $x + $y + $carry;
$carry = (int) ($sum / 10);
$current->next = new ListNode($sum % 10);
$current = $current->next;

if ($l1 !== null) {
$l1 = $l1->next;
}

if ($l2 !== null) {
$l2 = $l2->next;
}
}

if ($carry > 0) {
$current->next = new ListNode($carry);
}

return $dummy->next;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,35 @@ impl Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$max = 0;
for ($i = 0; $i < strlen($s); $i++) {
$chars = array();
$sub = '';
for ($j = $i; $j < strlen($s); $j++) {
if (in_array($s[$j], $chars)) {
break;
}
$sub .= $s[$j];
$chars[] = $s[$j];
}
if (strlen($sub) > $max) {
$max = strlen($sub);
}
}
return $max;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,35 @@ impl Solution {
}
```

### **PHP**

```php
class Solution {
/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$max = 0;
for ($i = 0; $i < strlen($s); $i++) {
$chars = array();
$sub = '';
for ($j = $i; $j < strlen($s); $j++) {
if (in_array($s[$j], $chars)) {
break;
}
$sub .= $s[$j];
$chars[] = $s[$j];
}
if (strlen($sub) > $max) {
$max = strlen($sub);
}
}
return $max;
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
class Solution {
/**
* @param String $s
* @return Integer
*/
function lengthOfLongestSubstring($s) {
$max = 0;
for ($i = 0; $i < strlen($s); $i++) {
$chars = array();
$sub = '';
for ($j = $i; $j < strlen($s); $j++) {
if (in_array($s[$j], $chars)) {
break;
}
$sub .= $s[$j];
$chars[] = $s[$j];
}
if (strlen($sub) > $max) {
$max = strlen($sub);
}
}
return $max;
}
}