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 81248ea

Browse files
feat: add php solution to lc problems: No.0031-0035 (#2536)
1 parent 5a4a9e6 commit 81248ea

File tree

15 files changed

+394
-0
lines changed

15 files changed

+394
-0
lines changed

‎solution/0000-0099/0031.Next Permutation/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,43 @@ public class Solution {
229229
}
230230
```
231231

232+
```php
233+
class Solution {
234+
/**
235+
* @param integer[] $nums
236+
* @return void
237+
*/
238+
239+
function nextPermutation(&$nums) {
240+
$n = count($nums);
241+
$i = $n - 2;
242+
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
243+
$i--;
244+
}
245+
if ($i >= 0) {
246+
$j = $n - 1;
247+
while ($j >= $i && $nums[$j] <= $nums[$i]) {
248+
$j--;
249+
}
250+
$temp = $nums[$i];
251+
$nums[$i] = $nums[$j];
252+
$nums[$j] = $temp;
253+
}
254+
$this->reverse($nums, $i + 1, $n - 1);
255+
}
256+
257+
function reverse(&$nums, $start, $end) {
258+
while ($start < $end) {
259+
$temp = $nums[$start];
260+
$nums[$start] = $nums[$end];
261+
$nums[$end] = $temp;
262+
$start++;
263+
$end--;
264+
}
265+
}
266+
}
267+
```
268+
232269
<!-- tabs:end -->
233270

234271
<!-- end -->

‎solution/0000-0099/0031.Next Permutation/README_EN.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,43 @@ public class Solution {
225225
}
226226
```
227227

228+
```php
229+
class Solution {
230+
/**
231+
* @param integer[] $nums
232+
* @return void
233+
*/
234+
235+
function nextPermutation(&$nums) {
236+
$n = count($nums);
237+
$i = $n - 2;
238+
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
239+
$i--;
240+
}
241+
if ($i >= 0) {
242+
$j = $n - 1;
243+
while ($j >= $i && $nums[$j] <= $nums[$i]) {
244+
$j--;
245+
}
246+
$temp = $nums[$i];
247+
$nums[$i] = $nums[$j];
248+
$nums[$j] = $temp;
249+
}
250+
$this->reverse($nums, $i + 1, $n - 1);
251+
}
252+
253+
function reverse(&$nums, $start, $end) {
254+
while ($start < $end) {
255+
$temp = $nums[$start];
256+
$nums[$start] = $nums[$end];
257+
$nums[$end] = $temp;
258+
$start++;
259+
$end--;
260+
}
261+
}
262+
}
263+
```
264+
228265
<!-- tabs:end -->
229266

230267
<!-- end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
3+
/**
4+
* @param integer[] $nums
5+
* @return void
6+
*/
7+
8+
function nextPermutation(&$nums) {
9+
$n = count($nums);
10+
$i = $n - 2;
11+
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
12+
$i--;
13+
}
14+
if ($i >= 0) {
15+
$j = $n - 1;
16+
while ($j >= $i && $nums[$j] <= $nums[$i]) {
17+
$j--;
18+
}
19+
$temp = $nums[$i];
20+
$nums[$i] = $nums[$j];
21+
$nums[$j] = $temp;
22+
}
23+
$this->reverse($nums, $i + 1, $n - 1);
24+
}
25+
26+
function reverse(&$nums, $start, $end) {
27+
while ($start < $end) {
28+
$temp = $nums[$start];
29+
$nums[$start] = $nums[$end];
30+
$nums[$end] = $temp;
31+
$start++;
32+
$end--;
33+
}
34+
}
35+
}

‎solution/0000-0099/0032.Longest Valid Parentheses/README.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,37 @@ var longestValidParentheses = function (s) {
385385
};
386386
```
387387

388+
```php
389+
class Solution {
390+
/**
391+
* @param string $s
392+
* @return integer
393+
*/
394+
395+
function longestValidParentheses($s) {
396+
$stack = [];
397+
$maxLength = 0;
398+
399+
array_push($stack, -1);
400+
for ($i = 0; $i < strlen($s); $i++) {
401+
if ($s[$i] === '(') {
402+
array_push($stack, $i);
403+
} else {
404+
array_pop($stack);
405+
406+
if (empty($stack)) {
407+
array_push($stack, $i);
408+
} else {
409+
$length = $i - end($stack);
410+
$maxLength = max($maxLength, $length);
411+
}
412+
}
413+
}
414+
return $maxLength;
415+
}
416+
}
417+
```
418+
388419
<!-- tabs:end -->
389420

390421
<!-- end -->

‎solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,37 @@ var longestValidParentheses = function (s) {
382382
};
383383
```
384384

385+
```php
386+
class Solution {
387+
/**
388+
* @param string $s
389+
* @return integer
390+
*/
391+
392+
function longestValidParentheses($s) {
393+
$stack = [];
394+
$maxLength = 0;
395+
396+
array_push($stack, -1);
397+
for ($i = 0; $i < strlen($s); $i++) {
398+
if ($s[$i] === '(') {
399+
array_push($stack, $i);
400+
} else {
401+
array_pop($stack);
402+
403+
if (empty($stack)) {
404+
array_push($stack, $i);
405+
} else {
406+
$length = $i - end($stack);
407+
$maxLength = max($maxLength, $length);
408+
}
409+
}
410+
}
411+
return $maxLength;
412+
}
413+
}
414+
```
415+
385416
<!-- tabs:end -->
386417

387418
<!-- end -->
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
/**
3+
* @param string $s
4+
* @return integer
5+
*/
6+
7+
function longestValidParentheses($s) {
8+
$stack = [];
9+
$maxLength = 0;
10+
11+
array_push($stack, -1);
12+
for ($i = 0; $i < strlen($s); $i++) {
13+
if ($s[$i] === '(') {
14+
array_push($stack, $i);
15+
} else {
16+
array_pop($stack);
17+
18+
if (empty($stack)) {
19+
array_push($stack, $i);
20+
} else {
21+
$length = $i - end($stack);
22+
$maxLength = max($maxLength, $length);
23+
}
24+
}
25+
}
26+
return $maxLength;
27+
}
28+
}

‎solution/0000-0099/0033.Search in Rotated Sorted Array/README.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,26 @@ var search = function (nums, target) {
254254
};
255255
```
256256

257+
```php
258+
class Solution {
259+
/**
260+
* @param integer[] $nums
261+
* @param integer $target
262+
* @return integer
263+
*/
264+
265+
function search($nums, $target) {
266+
$foundKey = -1;
267+
foreach ($nums as $key => $value) {
268+
if ($value === $target) {
269+
$foundKey = $key;
270+
}
271+
}
272+
return $foundKey;
273+
}
274+
}
275+
```
276+
257277
<!-- tabs:end -->
258278

259279
<!-- end -->

‎solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,26 @@ var search = function (nums, target) {
240240
};
241241
```
242242

243+
```php
244+
class Solution {
245+
/**
246+
* @param integer[] $nums
247+
* @param integer $target
248+
* @return integer
249+
*/
250+
251+
function search($nums, $target) {
252+
$foundKey = -1;
253+
foreach ($nums as $key => $value) {
254+
if ($value === $target) {
255+
$foundKey = $key;
256+
}
257+
}
258+
return $foundKey;
259+
}
260+
}
261+
```
262+
243263
<!-- tabs:end -->
244264

245265
<!-- end -->
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
/**
3+
* @param integer[] $nums
4+
* @param integer $target
5+
* @return integer
6+
*/
7+
8+
function search($nums, $target) {
9+
$foundKey = -1;
10+
foreach ($nums as $key => $value) {
11+
if ($value === $target) {
12+
$foundKey = $key;
13+
}
14+
}
15+
return $foundKey;
16+
}
17+
}

‎solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,33 @@ var searchRange = function (nums, target) {
233233
};
234234
```
235235

236+
```php
237+
class Solution {
238+
/**
239+
* @param integer[] $nums
240+
* @param integer $target
241+
* @return integer[]
242+
*/
243+
244+
function searchRange($nums, $target) {
245+
$min = -1;
246+
$max = -1;
247+
foreach ($nums as $key => $value) {
248+
if ($value == $target) {
249+
if ($min == -1) {
250+
$min = $key;
251+
}
252+
253+
if ($key > $max) {
254+
$max = $key;
255+
}
256+
}
257+
}
258+
return [$min, $max];
259+
}
260+
}
261+
```
262+
236263
<!-- tabs:end -->
237264

238265
<!-- end -->

0 commit comments

Comments
(0)

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