Skip to content

Commit 6b67498

Browse files
feat: add php solution to lc problems: No.0004-0010 (#2240)
1 parent 6234437 commit 6b67498

File tree

21 files changed

+567
-0
lines changed

21 files changed

+567
-0
lines changed

solution/0000-0099/0004.Median of Two Sorted Arrays/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,28 @@ public class Solution {
271271
}
272272
```
273273

274+
```php
275+
class Solution {
276+
/**
277+
* @param int[] $nums1
278+
* @param int[] $nums2
279+
* @return float
280+
*/
281+
282+
function findMedianSortedArrays($nums1, $nums2) {
283+
$arr = array_merge($nums1, $nums2);
284+
sort($arr);
285+
$cnt_arr = count($arr);
286+
287+
if ($cnt_arr % 2) {
288+
return $arr[$cnt_arr / 2];
289+
} else {
290+
return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2;
291+
}
292+
}
293+
}
294+
```
295+
274296
```nim
275297
import std/[algorithm, sequtils]
276298

solution/0000-0099/0004.Median of Two Sorted Arrays/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,28 @@ public class Solution {
265265
}
266266
```
267267

268+
```php
269+
class Solution {
270+
/**
271+
* @param int[] $nums1
272+
* @param int[] $nums2
273+
* @return float
274+
*/
275+
276+
function findMedianSortedArrays($nums1, $nums2) {
277+
$arr = array_merge($nums1, $nums2);
278+
sort($arr);
279+
$cnt_arr = count($arr);
280+
281+
if ($cnt_arr % 2) {
282+
return $arr[$cnt_arr / 2];
283+
} else {
284+
return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2;
285+
}
286+
}
287+
}
288+
```
289+
268290
```nim
269291
import std/[algorithm, sequtils]
270292
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
/**
3+
* @param int[] $nums1
4+
* @param int[] $nums2
5+
* @return float
6+
*/
7+
8+
function findMedianSortedArrays($nums1, $nums2) {
9+
$arr = array_merge($nums1, $nums2);
10+
sort($arr);
11+
$cnt_arr = count($arr);
12+
13+
if ($cnt_arr % 2) {
14+
return $arr[$cnt_arr / 2];
15+
} else {
16+
return ($arr[intdiv($cnt_arr, 2) - 1] + $arr[intdiv($cnt_arr, 2)]) / 2;
17+
}
18+
}
19+
}

solution/0000-0099/0005.Longest Palindromic Substring/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,42 @@ impl Solution {
410410
}
411411
```
412412

413+
```php
414+
class Solution {
415+
/**
416+
* @param string $s
417+
* @return string
418+
*/
419+
function longestPalindrome($s) {
420+
$start = 0;
421+
$maxLength = 0;
422+
423+
for ($i = 0; $i < strlen($s); $i++) {
424+
$len1 = $this->expandFromCenter($s, $i, $i);
425+
$len2 = $this->expandFromCenter($s, $i, $i + 1);
426+
427+
$len = max($len1, $len2);
428+
429+
if ($len > $maxLength) {
430+
$start = $i - intval(($len - 1) / 2);
431+
$maxLength = $len;
432+
}
433+
}
434+
435+
return substr($s, $start, $maxLength);
436+
}
437+
438+
function expandFromCenter($s, $left, $right) {
439+
while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) {
440+
$left--;
441+
$right++;
442+
}
443+
444+
return $right - $left - 1;
445+
}
446+
}
447+
```
448+
413449
<!-- tabs:end -->
414450

415451
<!-- end -->

solution/0000-0099/0005.Longest Palindromic Substring/README_EN.md

+36
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,42 @@ impl Solution {
404404
}
405405
```
406406

407+
```php
408+
class Solution {
409+
/**
410+
* @param string $s
411+
* @return string
412+
*/
413+
function longestPalindrome($s) {
414+
$start = 0;
415+
$maxLength = 0;
416+
417+
for ($i = 0; $i < strlen($s); $i++) {
418+
$len1 = $this->expandFromCenter($s, $i, $i);
419+
$len2 = $this->expandFromCenter($s, $i, $i + 1);
420+
421+
$len = max($len1, $len2);
422+
423+
if ($len > $maxLength) {
424+
$start = $i - intval(($len - 1) / 2);
425+
$maxLength = $len;
426+
}
427+
}
428+
429+
return substr($s, $start, $maxLength);
430+
}
431+
432+
function expandFromCenter($s, $left, $right) {
433+
while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) {
434+
$left--;
435+
$right++;
436+
}
437+
438+
return $right - $left - 1;
439+
}
440+
}
441+
```
442+
407443
<!-- tabs:end -->
408444

409445
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
/**
3+
* @param string $s
4+
* @return string
5+
*/
6+
function longestPalindrome($s) {
7+
$start = 0;
8+
$maxLength = 0;
9+
10+
for ($i = 0; $i < strlen($s); $i++) {
11+
$len1 = $this->expandFromCenter($s, $i, $i);
12+
$len2 = $this->expandFromCenter($s, $i, $i + 1);
13+
14+
$len = max($len1, $len2);
15+
16+
if ($len > $maxLength) {
17+
$start = $i - intval(($len - 1) / 2);
18+
$maxLength = $len;
19+
}
20+
}
21+
22+
return substr($s, $start, $maxLength);
23+
}
24+
25+
function expandFromCenter($s, $left, $right) {
26+
while ($left >= 0 && $right < strlen($s) && $s[$left] === $s[$right]) {
27+
$left--;
28+
$right++;
29+
}
30+
31+
return $right - $left - 1;
32+
}
33+
}

solution/0000-0099/0006.Zigzag Conversion/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,38 @@ var convert = function (s, numRows) {
411411
};
412412
```
413413

414+
```php
415+
class Solution {
416+
/**
417+
* @param string $s
418+
* @param int $numRows
419+
* @return string
420+
*/
421+
422+
function convert($s, $numRows) {
423+
if ($numRows == 1 || strlen($s) <= $numRows) {
424+
return $s;
425+
}
426+
427+
$result = '';
428+
$cycleLength = 2 * $numRows - 2;
429+
$n = strlen($s);
430+
431+
for ($i = 0; $i < $numRows; $i++) {
432+
for ($j = 0; $j + $i < $n; $j += $cycleLength) {
433+
$result .= $s[$j + $i];
434+
435+
if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) {
436+
$result .= $s[$j + $cycleLength - $i];
437+
}
438+
}
439+
}
440+
441+
return $result;
442+
}
443+
}
444+
```
445+
414446
<!-- tabs:end -->
415447

416448
<!-- end -->

solution/0000-0099/0006.Zigzag Conversion/README_EN.md

+32
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,38 @@ var convert = function (s, numRows) {
407407
};
408408
```
409409

410+
```php
411+
class Solution {
412+
/**
413+
* @param string $s
414+
* @param int $numRows
415+
* @return string
416+
*/
417+
418+
function convert($s, $numRows) {
419+
if ($numRows == 1 || strlen($s) <= $numRows) {
420+
return $s;
421+
}
422+
423+
$result = '';
424+
$cycleLength = 2 * $numRows - 2;
425+
$n = strlen($s);
426+
427+
for ($i = 0; $i < $numRows; $i++) {
428+
for ($j = 0; $j + $i < $n; $j += $cycleLength) {
429+
$result .= $s[$j + $i];
430+
431+
if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) {
432+
$result .= $s[$j + $cycleLength - $i];
433+
}
434+
}
435+
}
436+
437+
return $result;
438+
}
439+
}
440+
```
441+
410442
<!-- tabs:end -->
411443

412444
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
/**
3+
* @param string $s
4+
* @param int $numRows
5+
* @return string
6+
*/
7+
8+
function convert($s, $numRows) {
9+
if ($numRows == 1 || strlen($s) <= $numRows) {
10+
return $s;
11+
}
12+
13+
$result = '';
14+
$cycleLength = 2 * $numRows - 2;
15+
$n = strlen($s);
16+
17+
for ($i = 0; $i < $numRows; $i++) {
18+
for ($j = 0; $j + $i < $n; $j += $cycleLength) {
19+
$result .= $s[$j + $i];
20+
21+
if ($i != 0 && $i != $numRows - 1 && $j + $cycleLength - $i < $n) {
22+
$result .= $s[$j + $cycleLength - $i];
23+
}
24+
}
25+
}
26+
27+
return $result;
28+
}
29+
}

solution/0000-0099/0007.Reverse Integer/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,36 @@ int reverse(int x) {
193193
}
194194
```
195195
196+
```php
197+
class Solution {
198+
/**
199+
* @param int $x
200+
* @return int
201+
*/
202+
203+
function reverse($x) {
204+
$isNegative = $x < 0;
205+
$x = abs($x);
206+
207+
$reversed = 0;
208+
209+
while ($x > 0) {
210+
$reversed = $reversed * 10 + ($x % 10);
211+
$x = (int) ($x / 10);
212+
}
213+
214+
if ($isNegative) {
215+
$reversed *= -1;
216+
}
217+
if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) {
218+
return 0;
219+
}
220+
221+
return $reversed;
222+
}
223+
}
224+
```
225+
196226
<!-- tabs:end -->
197227

198228
<!-- end -->

solution/0000-0099/0007.Reverse Integer/README_EN.md

+30
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,36 @@ int reverse(int x) {
181181
}
182182
```
183183
184+
```php
185+
class Solution {
186+
/**
187+
* @param int $x
188+
* @return int
189+
*/
190+
191+
function reverse($x) {
192+
$isNegative = $x < 0;
193+
$x = abs($x);
194+
195+
$reversed = 0;
196+
197+
while ($x > 0) {
198+
$reversed = $reversed * 10 + ($x % 10);
199+
$x = (int) ($x / 10);
200+
}
201+
202+
if ($isNegative) {
203+
$reversed *= -1;
204+
}
205+
if ($reversed < -pow(2, 31) || $reversed > pow(2, 31) - 1) {
206+
return 0;
207+
}
208+
209+
return $reversed;
210+
}
211+
}
212+
```
213+
184214
<!-- tabs:end -->
185215

186216
<!-- end -->

0 commit comments

Comments
 (0)