Skip to content
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

feat: add php solution to lc problems: No.0031-0035 #2536

Merged
merged 19 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
37 changes: 37 additions & 0 deletions solution/0000-0099/0031.Next Permutation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,43 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param integer[] $nums
* @return void
*/

function nextPermutation(&$nums) {
$n = count($nums);
$i = $n - 2;
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
$i--;
}
if ($i >= 0) {
$j = $n - 1;
while ($j >= $i && $nums[$j] <= $nums[$i]) {
$j--;
}
$temp = $nums[$i];
$nums[$i] = $nums[$j];
$nums[$j] = $temp;
}
$this->reverse($nums, $i + 1, $n - 1);
}

function reverse(&$nums, $start, $end) {
while ($start < $end) {
$temp = $nums[$start];
$nums[$start] = $nums[$end];
$nums[$end] = $temp;
$start++;
$end--;
}
}
}
```

<!-- tabs:end -->

<!-- end -->
37 changes: 37 additions & 0 deletions solution/0000-0099/0031.Next Permutation/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,43 @@ public class Solution {
}
```

```php
class Solution {
/**
* @param integer[] $nums
* @return void
*/

function nextPermutation(&$nums) {
$n = count($nums);
$i = $n - 2;
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
$i--;
}
if ($i >= 0) {
$j = $n - 1;
while ($j >= $i && $nums[$j] <= $nums[$i]) {
$j--;
}
$temp = $nums[$i];
$nums[$i] = $nums[$j];
$nums[$j] = $temp;
}
$this->reverse($nums, $i + 1, $n - 1);
}

function reverse(&$nums, $start, $end) {
while ($start < $end) {
$temp = $nums[$start];
$nums[$start] = $nums[$end];
$nums[$end] = $temp;
$start++;
$end--;
}
}
}
```

<!-- tabs:end -->

<!-- end -->
35 changes: 35 additions & 0 deletions solution/0000-0099/0031.Next Permutation/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {

/**
* @param integer[] $nums
* @return void
*/

function nextPermutation(&$nums) {
$n = count($nums);
$i = $n - 2;
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
$i--;
}
if ($i >= 0) {
$j = $n - 1;
while ($j >= $i && $nums[$j] <= $nums[$i]) {
$j--;
}
$temp = $nums[$i];
$nums[$i] = $nums[$j];
$nums[$j] = $temp;
}
$this->reverse($nums, $i + 1, $n - 1);
}

function reverse(&$nums, $start, $end) {
while ($start < $end) {
$temp = $nums[$start];
$nums[$start] = $nums[$end];
$nums[$end] = $temp;
$start++;
$end--;
}
}
}
31 changes: 31 additions & 0 deletions solution/0000-0099/0032.Longest Valid Parentheses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,37 @@ var longestValidParentheses = function (s) {
};
```

```php
class Solution {
/**
* @param string $s
* @return integer
*/

function longestValidParentheses($s) {
$stack = [];
$maxLength = 0;

array_push($stack, -1);
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] === '(') {
array_push($stack, $i);
} else {
array_pop($stack);

if (empty($stack)) {
array_push($stack, $i);
} else {
$length = $i - end($stack);
$maxLength = max($maxLength, $length);
}
}
}
return $maxLength;
}
}
```

<!-- tabs:end -->

<!-- end -->
31 changes: 31 additions & 0 deletions solution/0000-0099/0032.Longest Valid Parentheses/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,37 @@ var longestValidParentheses = function (s) {
};
```

```php
class Solution {
/**
* @param string $s
* @return integer
*/

function longestValidParentheses($s) {
$stack = [];
$maxLength = 0;

array_push($stack, -1);
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] === '(') {
array_push($stack, $i);
} else {
array_pop($stack);

if (empty($stack)) {
array_push($stack, $i);
} else {
$length = $i - end($stack);
$maxLength = max($maxLength, $length);
}
}
}
return $maxLength;
}
}
```

<!-- tabs:end -->

<!-- end -->
28 changes: 28 additions & 0 deletions solution/0000-0099/0032.Longest Valid Parentheses/Solution.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
/**
* @param string $s
* @return integer
*/

function longestValidParentheses($s) {
$stack = [];
$maxLength = 0;

array_push($stack, -1);
for ($i = 0; $i < strlen($s); $i++) {
if ($s[$i] === '(') {
array_push($stack, $i);
} else {
array_pop($stack);

if (empty($stack)) {
array_push($stack, $i);
} else {
$length = $i - end($stack);
$maxLength = max($maxLength, $length);
}
}
}
return $maxLength;
}
}
20 changes: 20 additions & 0 deletions solution/0000-0099/0033.Search in Rotated Sorted Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,26 @@ var search = function (nums, target) {
};
```

```php
class Solution {
/**
* @param integer[] $nums
* @param integer $target
* @return integer
*/

function search($nums, $target) {
$foundKey = -1;
foreach ($nums as $key => $value) {
if ($value === $target) {
$foundKey = $key;
}
}
return $foundKey;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@ var search = function (nums, target) {
};
```

```php
class Solution {
/**
* @param integer[] $nums
* @param integer $target
* @return integer
*/

function search($nums, $target) {
$foundKey = -1;
foreach ($nums as $key => $value) {
if ($value === $target) {
$foundKey = $key;
}
}
return $foundKey;
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
/**
* @param integer[] $nums
* @param integer $target
* @return integer
*/

function search($nums, $target) {
$foundKey = -1;
foreach ($nums as $key => $value) {
if ($value === $target) {
$foundKey = $key;
}
}
return $foundKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,33 @@ var searchRange = function (nums, target) {
};
```

```php
class Solution {
/**
* @param integer[] $nums
* @param integer $target
* @return integer[]
*/

function searchRange($nums, $target) {
$min = -1;
$max = -1;
foreach ($nums as $key => $value) {
if ($value == $target) {
if ($min == -1) {
$min = $key;
}

if ($key > $max) {
$max = $key;
}
}
}
return [$min, $max];
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,33 @@ var searchRange = function (nums, target) {
};
```

```php
class Solution {
/**
* @param integer[] $nums
* @param integer $target
* @return integer[]
*/

function searchRange($nums, $target) {
$min = -1;
$max = -1;
foreach ($nums as $key => $value) {
if ($value == $target) {
if ($min == -1) {
$min = $key;
}

if ($key > $max) {
$max = $key;
}
}
}
return [$min, $max];
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
/**
* @param integer[] $nums
* @param integer $target
* @return integer[]
*/

function searchRange($nums, $target) {
$min = -1;
$max = -1;
foreach ($nums as $key => $value) {
if ($value == $target) {
if ($min == -1) {
$min = $key;
}

if ($key > $max) {
$max = $key;
}
}
}
return [$min, $max];
}
}
Loading