10000 Added tasks 31-46 · pytdev/LeetCode-in-Php@1d4535a · GitHub
[go: up one dir, main page]

Skip to content

Commit 1d4535a

Browse files
authored
Added tasks 31-46
1 parent c07ee44 commit 1d4535a

File tree

43 files changed

+949
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+949
-13
lines changed

src/main/php/g0001_0100/s0001_two_sum/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Solution {
1212
* @param Integer $target
1313
* @return Integer[]
1414
*/
15-
function twoSum($nums, $target) {
15+
public function twoSum($nums, $target) {
1616
$ind = [];
1717
for ($i = 0; $i < count($nums); ++$i) {
1818
$complement = $target - $nums[$i];

src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Solution {
2525
* @param ListNode $l2
2626
* @return ListNode
2727
*/
28-
function addTwoNumbers($l1, $l2) {
28+
public function addTwoNumbers($l1, $l2) {
2929
$dummyHead = new ListNode(0);
3030
$p = $l1;
3131
$q = $l2;

src/main/php/g0001_0100/s0004_median_of_two_sorted_arrays/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Solution {
1111
* @param Integer[] $nums2
1212
* @return Float
1313
*/
14-
function findMedianSortedArrays($nums1, $nums2) {
14+
public function findMedianSortedArrays($nums1, $nums2) {
1515
$nums3 = [];
1616
$sum = count($nums1) + count($nums2);
1717
$med = ($sum / 2);

src/main/php/g0001_0100/s0005_longest_palindromic_substring/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Solution {
1212
* @param String $s
1313
* @return String
1414
*/
15-
function longestPalindrome($s): string {
15+
public function longestPalindrome($s): string {
1616
if (($length = strlen($s)) <= 1) {
1717
return $s;
1818
}

src/main/php/g0001_0100/s0006_zigzag_conversion/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class Solution {
1010
* @param Integer $numRows
1111
* @return String
1212
*/
13-
function convert($s, $numRows) {
13+
public function convert($s, $numRows) {
1414
if ($numRows == 1) {
1515
return $s;
1616
}

src/main/php/g0001_0100/s0007_reverse_integer/Solution.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
// #2023_12_03_Time_3_ms_(90.99%)_Space_19.2_MB_(44.23%)
77

88
class Solution {
9-
function reverse($x) {
9+
/**
10+
* @param Integer $x
11+
* @return Integer
12+
*/
13+
public function reverse($x) {
1014
$ls_negative = '';
1115
if (strpos($x, '-') === 0) {
1216
$ls_negative = substr($x, 0, 1);

src/main/php/g0001_0100/s0008_string_to_integer_atoi/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Solution {
99
* @param String $s
1010
* @return Integer
1111
*/
12-
function myAtoi($s) {
12+
public function myAtoi($s) {
1313
$pos = 0;
1414
$res = 0;
1515
for ($i = 0; $i < strlen($s); $i++) {

src/main/php/g0001_0100/s0009_palindrome_number/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Solution {
99
* @param Integer $x
1010
* @return Boolean
1111
*/
12-
function isPalindrome($x) {
12+
public function isPalindrome($x) {
1313
if ($x < 0) {
1414
return false;
1515
}

src/main/php/g0001_0100/s0019_remove_nth_node_from_end_of_list/Solution.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
class Solution {
2424
private $n;
2525

26+
/**
27+
* @param ListNode $head
28+
* @param Integer $n
29+
* @return ListNode
30+
*/
2631
public function removeNthFromEnd($head, $n) {
2732
$this->n = $n;
2833
$node = new ListNode(0, $head);

src/main/php/g0001_0100/s0022_generate_parentheses/Solution.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class Solution {
1111
* @param Integer $n
1212
* @return String[]
1313
*/
14-
function generateParenthesis(int $n): array {
14+
public function generateParenthesis(int $n): array {
1515
$sb = '';
1616
$ans = [];
1717
return $this->generate($sb, $ans, $n, $n);
1818
}
1919

20-
function generate(string &$sb, array &$ans, int $open, int $close): array {
20+
private function generate(string &$sb, array &$ans, int $open, int $close): array {
2121
if ($open === 0 && $close === 0) {
2222
$ans[] = $sb;
2323
return $ans;

src/main/php/g0001_0100/s0023_merge_k_sorted_lists/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Solution {
2424
* @param ListNode[] $lists
2525
* @return ListNode
2626
*/
27-
function mergeKLists($lists) {
27+
public function mergeKLists($lists) {
2828
$heap = new \SplMinHeap();
2929
$head = [];
3030
foreach ($lists as $list) {

src/main/php/g0001_0100/s0024_swap_nodes_in_pairs/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class Solution {
2424
* @param ListNode $head
2525
* @return ListNode
2626
*/
27-
function swapPairs($head) {
27+
public function swapPairs($head) {
2828
$output = new ListNode(0);
2929
$output->next = $head;
3030
$point = $output;

src/main/php/g0001_0100/s0025_reverse_nodes_in_k_group/Solution.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Solution {
2525
* @param Integer $k
2626
* @return ListNode
2727
*/
28-
function reverseKGroup($head, $k) {
28+
public function reverseKGroup($head, $k) {
2929
if ($this->countNodesInList($head) < $k || $head->next == null || $head == null) {
3030
return $head;
3131
} else {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0031_next_permutation;
4+
5+
// #Medium #Top_100_Liked_Questions #Array #Two_Pointers #Big_O_Time_O(n)_Space_O(1)
6+
// #2023_12_09_Time_7_ms_(86.36%)_Space_18.6_MB_(100.00%)
7+
8+
class Solution {
9+
/**
10+
* @param Integer[] $nums
11+
* @return NULL
12+
*/
13+
public function nextPermutation(&$nums) {
14+
if ($nums == null || count($nums) <= 1) {
15+
return;
16+
}
17+
$i = count($nums) - 2;
18+
while ($i >= 0 && $nums[$i] >= $nums[$i + 1]) {
19+
$i--;
20+
}
21+
if ($i >= 0) {
22+
$j = count($nums) - 1;
23+
while ($nums[$j] <= $nums[$i]) {
24+
$j--;
25+
}
26+
$this->swap($nums, $i, $j);
27+
}
28+
$this->reverse($nums, $i + 1, count($nums) - 1);
29+
}
30+
31+
private function swap(&$nums, $i, $j) {
32+
$temp = $nums[$i];
33+
$nums[$i] = $nums[$j];
34+
$nums[$j] = $temp;
35+
}
36+
37+
private function reverse(&$nums, $i, $j) {
38+
while ($i < $j) {
39+
$this->swap($nums, $i++, $j--);
40+
}
41+
}
42+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
31\. Next Permutation
2+
3+
Medium
4+
5+
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers.
6+
7+
If such an arrangement is not possible, it must rearrange it as the lowest possible order (i.e., sorted in ascending order).
8+
9+
The replacement must be **[in place](http://en.wikipedia.org/wiki/In-place_algorithm)** and use only constant extra memory.
10+
11+
**Example 1:**
12+
13+
**Input:** nums = [1,2,3]
14+
15+
**Output:** [1,3,2]
16+
17+
**Example 2:**
18+
19+
**Input:** nums = [3,2,1]
20+
21+
**Output:** [1,2,3]
22+
23+
**Example 3:**
24+
25+
**Input:** nums = [1,1,5]
26+
27+
**Output:** [1,5,1]
28+
29+
**Example 4:**
30+
31+
**Input:** nums = [1]
32+
33+
**Output:** [1]
34+
35+
**Constraints:**
36+
37+
* `1 <= nums.length <= 100`
38+
* `0 <= nums[i] <= 100`
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0032_longest_valid_parentheses;
4+
5+
// #Hard #Top_100_Liked_Questions #String #Dynamic_Programming #Stack #Big_O_Time_O(n)_Space_O(1)
6+
// #2023_12_09_Time_8_ms_(67.65%)_Space_19.2_MB_(85.29%)
7+
8+
class Solution {
9+
/**
10+
* @param String $s
11+
* @return Integer
12+
*/
13+
public function longestValidParentheses($s) {
14+
$max = 0;
15+
$left = 0;
16+
$right = 0;
17+
$n = strlen($s);
18+
for ($i = 0; $i < $n; $i++) {
19+
$ch = $s[$i];
20+
if ($ch == '(') {
21+
$left++;
22+
} else {
23+
$right++;
24+
}
25+
if ($right > $left) {
26+
$left = 0;
27+
$right = 0;
28+
}
29+
if ($left == $right) {
30+
$max = max($max, $left + $right);
31+
}
32+
}
33+
$left = 0;
34+
$right = 0;
35+
for ($i = $n - 1; $i >= 0; $i--) {
36+
$ch = $s[$i];
37+
if ($ch == '(') {
38+
$left++;
39+
} else {
40+
$right++;
41+
}
42+
if ($left > $right) {
43+
$left = 0;
44+
$right = 0;
45+
}
46+
if ($left == $right) {
47+
$max = max($max, $left + $right);
48+
}
49+
}
50+
return $max;
51+
}
52+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
32\. Longest Valid Parentheses
2+
3+
Hard
4+
5+
Given a string containing just the characters `'('` and `')'`, find the length of the longest valid (well-formed) parentheses substring.
6+
7+
**Example 1:**
8+
9+
**Input:** s = "(()"
10+
11+
**Output:** 2
12+
13+
**Explanation:** The longest valid parentheses substring is "()".
14+
15+
**Example 2:**
16+
17+
**Input:** s = ")()())"
18+
19+
**Output:** 4
20+
21+
**Explanation:** The longest valid parentheses substring is "()()".
22+
23+
**Example 3:**
24+
25+
**Input:** s = ""
26+
27+
**Output:* 10000 * 0
28+
29+
**Constraints:**
30+
31+
* <code>0 <= s.length <= 3 * 10<sup>4</sup></code>
32+
* `s[i]` is `'('`, or `')'`.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace leetcode\g0001_0100\s0033_search_in_rotated_sorted_array;
4+
5+
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Binary_Search
6+
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_11 #Level_2_Day_8_Binary_Search
7+
// #Udemy_Binary_Search #Big_O_Time_O(log_n)_Space_O(1)
8+
// #2023_12_09_Time_7_ms_(83.17%)_Space_19.1_MB_(83.17%)
9+
10+
class Solution {
11+
/**
12+
* @param Integer[] $nums
13+
* @param Integer $target
14+
* @return Integer
15+
*/
16+
public function search($nums, $target) {
17+
$lo = 0;
18+
$hi = count($nums) - 1;
19+
while ($lo <= $hi) {
20+
$mid = (($hi - $lo) >> 1) + $lo;
21+
if ($target == $nums[$mid]) {
22+
return $mid;
23+
}
24+
if ($nums[$lo] <= $nums[$mid]) {
25+
if ($nums[$lo] <= $target && $target <= $nums[$mid]) {
26+
$hi = $mid - 1;
27+
} else {
28+
$lo = $mid + 1;
29+
}
30+
} elseif ($nums[$mid] <= $target && $target <= $nums[$hi]) {
31+
$lo = $mid + 1;
32+
} else {
33+
$hi = $mid - 1;
34+
}
35+
}
36+
return -1;
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
33\. Search in Rotated Sorted Array
2+
3+
Medium
4+
5+
There is an integer array `nums` sorted in ascending order (with **distinct** values).
6+
7+
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]`.
8+
9+
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`.
10+
11+
You must write an algorithm with `O(log n)` runtime complexity.
12+
13+
**Example 1:**
14+
15+
**Input:** nums = [4,5,6,7,0,1,2], target = 0
16+
17+
**Output:** 4
18+
19+
**Example 2:**
20+
21+
**Input:** nums = [4,5,6,7,0,1,2], target = 3
22+
23+
**Output:** -1
24+
25+
**Example 3:**
26+
27+
**Input:** nums = [1], target = 0
28+
29+
**Output:** -1
30+
31+
**Constraints:**
32+
33+
* `1 <= nums.length <= 5000`
34+
* <code>-10<sup>4</sup> <= nums[i] <= 10<sup>4</sup></code>
35+
* All values of `nums` are **unique**.
36+
* `nums` is an ascending array that is possibly rotated.
37+
* <code>-10<sup>4</sup> <= target <= 10<sup>4</sup></code>

0 commit comments

Comments
 (0)
0