Skip to content

Commit

Permalink
Added tasks 31-46
Browse files Browse the repository at this point in the history
  • Loading branch information
javadev authored Dec 9, 2023
1 parent c07ee44 commit 1d4535a
Show file tree
Hide file tree
Showing 43 changed files with 949 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/php/g0001_0100/s0001_two_sum/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Solution {
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target) {
public function twoSum($nums, $target) {
$ind = [];
for ($i = 0; $i < count($nums); ++$i) {
$complement = $target - $nums[$i];
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/g0001_0100/s0002_add_two_numbers/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Solution {
* @param ListNode $l2
* @return ListNode
*/
function addTwoNumbers($l1, $l2) {
public function addTwoNumbers($l1, $l2) {
$dummyHead = new ListNode(0);
$p = $l1;
$q = $l2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Solution {
* @param Integer[] $nums2
* @return Float
*/
function findMedianSortedArrays($nums1, $nums2) {
public function findMedianSortedArrays($nums1, $nums2) {
$nums3 = [];
$sum = count($nums1) + count($nums2);
$med = ($sum / 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Solution {
* @param String $s
* @return String
*/
function longestPalindrome($s): string {
public function longestPalindrome($s): string {
if (($length = strlen($s)) <= 1) {
return $s;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Solution {
* @param Integer $numRows
* @return String
*/
function convert($s, $numRows) {
public function convert($s, $numRows) {
if ($numRows == 1) {
return $s;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/php/g0001_0100/s0007_reverse_integer/Solution.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
// #2023_12_03_Time_3_ms_(90.99%)_Space_19.2_MB_(44.23%)

class Solution {
function reverse($x) {
/**
* @param Integer $x
* @return Integer
*/
public function reverse($x) {
$ls_negative = '';
if (strpos($x, '-') === 0) {
$ls_negative = substr($x, 0, 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Solution {
* @param String $s
* @return Integer
*/
function myAtoi($s) {
public function myAtoi($s) {
$pos = 0;
$res = 0;
for ($i = 0; $i < strlen($s); $i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Solution {
* @param Integer $x
* @return Boolean
*/
function isPalindrome($x) {
public function isPalindrome($x) {
if ($x < 0) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
class Solution {
private $n;

/**
* @param ListNode $head
* @param Integer $n
* @return ListNode
*/
public function removeNthFromEnd($head, $n) {
$this->n = $n;
$node = new ListNode(0, $head);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class Solution {
* @param Integer $n
* @return String[]
*/
function generateParenthesis(int $n): array {
public function generateParenthesis(int $n): array {
$sb = '';
$ans = [];
return $this->generate($sb, $ans, $n, $n);
}

function generate(string &$sb, array &$ans, int $open, int $close): array {
private function generate(string &$sb, array &$ans, int $open, int $close): array {
if ($open === 0 && $close === 0) {
$ans[] = $sb;
return $ans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Solution {
* @param ListNode[] $lists
* @return ListNode
*/
function mergeKLists($lists) {
public function mergeKLists($lists) {
$heap = new \SplMinHeap();
$head = [];
foreach ($lists as $list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Solution {
* @param ListNode $head
* @return ListNode
*/
function swapPairs($head) {
public function swapPairs($head) {
$output = new ListNode(0);
$output->next = $head;
$point = $output;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Solution {
* @param Integer $k
* @return ListNode
*/
function reverseKGroup($head, $k) {
public function reverseKGroup($head, $k) {
if ($this->countNodesInList($head) < $k || $head->next == null || $head == null) {
return $head;
} else {
Expand Down
42 changes: 42 additions & 0 deletions src/main/php/g0001_0100/s0031_next_permutation/Solution.php
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--);
}
}
}
38 changes: 38 additions & 0 deletions src/main/php/g0001_0100/s0031_next_permutation/readme.md
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`
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 src/main/php/g0001_0100/s0032_longest_valid_parentheses/readme.md
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 `')'`.
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;
}
}
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>
Loading

0 comments on commit 1d4535a

Please sign in to comment.