forked from LeetCode-in-Php/LeetCode-in-Php
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
949 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/php/g0001_0100/s0031_next_permutation/Solution.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace leetcode\g0001_0100\s0031_next_permutation; | ||
|
||
// #Medium #Top_100_Liked_Questions #Array #Two_Pointers #Big_O_Time_O(n)_Space_O(1) | ||
// #2023_12_09_Time_7_ms_(86.36%)_Space_18.6_MB_(100.00%) | ||
|
||
class Solution { | ||
/** | ||
* @param Integer[] $nums | ||
* @return NULL | ||
*/ | ||
public function nextPermutation(&$nums) { | ||
if ($nums == null || count($nums) <= 1) { | ||
return; | ||
} | ||
$i = count($nums) - 2; | ||
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) { | ||
$i--; | ||
} | ||
if ($i >= 0) { | ||
$j = count($nums) - 1; | ||
while ($nums[$j] <= $nums[$i]) { | ||
$j--; | ||
} | ||
$this->swap($nums, $i, $j); | ||
} | ||
$this->reverse($nums, $i + 1, count($nums) - 1); | ||
} | ||
|
||
private function swap(&$nums, $i, $j) { | ||
$temp = $nums[$i]; | ||
$nums[$i] = $nums[$j]; | ||
$nums[$j] = $temp; | ||
} | ||
|
||
private function reverse(&$nums, $i, $j) { | ||
while ($i < $j) { | ||
$this->swap($nums, $i++, $j--); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
31\. Next Permutation | ||
|
||
Medium | ||
|
||
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers. | ||
|
||
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order). | ||
|
||
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [1,2,3] | ||
|
||
**Output:** [1,3,2] | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [3,2,1] | ||
|
||
**Output:** [1,2,3] | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [1,1,5] | ||
|
||
**Output:** [1,5,1] | ||
|
||
**Example 4:** | ||
|
||
**Input:** nums = [1] | ||
|
||
**Output:** [1] | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 100` | ||
* `0 <= nums[i] <= 100` |
52 changes: 52 additions & 0 deletions
52
src/main/php/g0001_0100/s0032_longest_valid_parentheses/Solution.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace leetcode\g0001_0100\s0032_longest_valid_parentheses; | ||
|
||
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1) | ||
// #2023_12_09_Time_8_ms_(67.65%)_Space_19.2_MB_(85.29%) | ||
|
||
class Solution { | ||
/** | ||
* @param String $s | ||
* @return Integer | ||
*/ | ||
public function longestValidParentheses($s) { | ||
$max = 0; | ||
$left = 0; | ||
$right = 0; | ||
$n = strlen($s); | ||
for ($i = 0; $i < $n; $i++) { | ||
$ch = $s[$i]; | ||
if ($ch == '(') { | ||
$left++; | ||
} else { | ||
$right++; | ||
} | ||
if ($right > $left) { | ||
$left = 0; | ||
$right = 0; | ||
} | ||
if ($left == $right) { | ||
$max = max($max, $left + $right); | ||
} | ||
} | ||
$left = 0; | ||
$right = 0; | ||
for ($i = $n - 1; $i >= 0; $i--) { | ||
$ch = $s[$i]; | ||
if ($ch == '(') { | ||
$left++; | ||
} else { | ||
$right++; | ||
} | ||
if ($left > $right) { | ||
$left = 0; | ||
$right = 0; | ||
} | ||
if ($left == $right) { | ||
$max = max($max, $left + $right); | ||
} | ||
} | ||
return $max; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/php/g0001_0100/s0032_longest_valid_parentheses/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
32\. Longest Valid Parentheses | ||
|
||
Hard | ||
|
||
Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring. | ||
|
||
**Example 1:** | ||
|
||
**Input:** s = "(()" | ||
|
||
**Output:** 2 | ||
|
||
**Explanation:** The longest valid parentheses substring is "()". | ||
|
||
**Example 2:** | ||
|
||
**Input:** s = ")()())" | ||
|
||
**Output:** 4 | ||
|
||
**Explanation:** The longest valid parentheses substring is "()()". | ||
|
||
**Example 3:** | ||
|
||
**Input:** s = "" | ||
|
||
**Output:** 0 | ||
|
||
**Constraints:** | ||
|
||
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code> | ||
* `s[i]` is `'('`, or `')'`. |
38 changes: 38 additions & 0 deletions
38
src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/Solution.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace leetcode\g0001_0100\s0033_search_in_rotated_sorted_array; | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search | ||
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_11 #Level_2_Day_8_Binary_Search | ||
// #Udemy_Binary_Search #Big_O_Time_O(log_n)_Space_O(1) | ||
// #2023_12_09_Time_7_ms_(83.17%)_Space_19.1_MB_(83.17%) | ||
|
||
class Solution { | ||
/** | ||
* @param Integer[] $nums | ||
* @param Integer $target | ||
* @return Integer | ||
*/ | ||
public function search($nums, $target) { | ||
$lo = 0; | ||
$hi = count($nums) - 1; | ||
while ($lo <= $hi) { | ||
$mid = (($hi - $lo) >> 1) + $lo; | ||
if ($target == $nums[$mid]) { | ||
return $mid; | ||
} | ||
if ($nums[$lo] <= $nums[$mid]) { | ||
if ($nums[$lo] <= $target && $target <= $nums[$mid]) { | ||
$hi = $mid - 1; | ||
} else { | ||
$lo = $mid + 1; | ||
} | ||
} elseif ($nums[$mid] <= $target && $target <= $nums[$hi]) { | ||
$lo = $mid + 1; | ||
} else { | ||
$hi = $mid - 1; | ||
} | ||
} | ||
return -1; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/php/g0001_0100/s0033_search_in_rotated_sorted_array/readme.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
33\. Search in Rotated Sorted Array | ||
|
||
Medium | ||
|
||
There is an integer array `nums` sorted in ascending order (with **distinct** values). | ||
|
||
Prior to being passed to your function, `nums` is **possibly rotated** at an unknown pivot index `k` (`1 <= k < nums.length`) such that the resulting array is `[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]]` (**0-indexed**). For example, `[0,1,2,4,5,6,7]` might be rotated at pivot index `3` and become `[4,5,6,7,0,1,2]`. | ||
|
||
Given the array `nums` **after** the possible rotation and an integer `target`, return _the index of_ `target` _if it is in_ `nums`_, or_ `-1` _if it is not in_ `nums`. | ||
|
||
You must write an algorithm with `O(log n)` runtime complexity. | ||
|
||
**Example 1:** | ||
|
||
**Input:** nums = [4,5,6,7,0,1,2], target = 0 | ||
|
||
**Output:** 4 | ||
|
||
**Example 2:** | ||
|
||
**Input:** nums = [4,5,6,7,0,1,2], target = 3 | ||
|
||
**Output:** -1 | ||
|
||
**Example 3:** | ||
|
||
**Input:** nums = [1], target = 0 | ||
|
||
**Output:** -1 | ||
|
||
**Constraints:** | ||
|
||
* `1 <= nums.length <= 5000` | ||
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code> | ||
* All values of `nums` are **unique**. | ||
* `nums` is an ascending array that is possibly rotated. | ||
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code> |
Oops, something went wrong.