Skip to content

Commit

Permalink
Update links
Browse files Browse the repository at this point in the history
  • Loading branch information
RamyHakam committed Dec 2, 2021
1 parent e6d511d commit f9fbaa2
Show file tree
Hide file tree
Showing 12 changed files with 262 additions and 154 deletions.
6 changes: 5 additions & 1 deletion ArrayProblems/MergeIntervals.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Hakam\LeetCodePhp\ArrayProblems;

/**
* LeetCode Problem Link : https://leetcode.com/problems/merge-intervals
*/
class MergeIntervals
{
/**
* @param Integer[][] $intervals
* @return Integer[][]
*/
function merge($intervals) {
public function merge(array $intervals): array
{
usort($intervals,function($i,$j){
if($i[0] === $j[0])
{
Expand Down
10 changes: 6 additions & 4 deletions ArrayProblems/MergeSortedArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

use SplMinHeap;

/**
* LeetCode Problem Link : https://leetcode.com/problems/merge-sorted-array
*/
class MergeSortedArray
{
/**
* @param Integer[] $nums1
* @param Integer $m
* @param Integer[] $nums2
* @param Integer $n
* @return NULL
* @return void
*/

function mergeByHeap(&$nums1, $m, $nums2, $n)
public function mergeByHeap(array &$nums1, int $m, array $nums2, int $n): void
{
$minHeap = new SplMinHeap();
for ($index = 0; $index < $m + $n; $index++) {
Expand All @@ -28,10 +31,9 @@ function mergeByHeap(&$nums1, $m, $nums2, $n)
for ($index = 0; $index < $m + $n; $index++) {
$nums1[$index] = $minHeap->extract();
}
return null;
}

function mergeByPointers(&$nums1, $m, $nums2, $n)
public function mergeByPointers(&$nums1, $m, $nums2, $n): void
{
for ($i = 0; $i < $m; $i++) {
$nums1Copy[$i] = $nums1[$i];
Expand Down
8 changes: 6 additions & 2 deletions ArrayProblems/MoveZeroes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

namespace Hakam\LeetCodePhp\ArrayProblems;

/**
* LeetCode Problem Link : https://leetcode.com/problems/move-zeroes
*/
class MoveZeroes
{
/**
* @param Integer[] $nums
* @return NULL
* @return void
*/
function moveZeroes(&$nums) {
public function moveZeroes(array &$nums): void
{
foreach ($nums as $i => $iValue) {
if ($iValue === 0) {
unset($nums[$i]);
Expand Down
3 changes: 3 additions & 0 deletions ArrayProblems/RemoveElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Hakam\LeetCodePhp\ArrayProblems;

/**
* LeetCode Problem Link : https://leetcode.com/problems/remove-element
*/
class RemoveElement
{
/**
Expand Down
9 changes: 6 additions & 3 deletions ArrayProblems/ThreeSum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,23 @@

namespace Hakam\LeetCodePhp\ArrayProblems;

/**
* LeetCode Problem Link: https://leetcode.com/problems/3sum
*/
class ThreeSum
{
/**
* @param Integer[] $nums
* @return Integer[][]
*/
function threeSum($nums)
public function threeSum(array $nums): array
{
sort($nums);
$count = count($nums);
$listOf3Sum = [];
for ($index = 0; $index < $count; $index++) {
$number = $nums[$index];
if ($index > 0 && $number == $nums[$index - 1]) {
if ($index > 0 && $number === $nums[$index - 1]) {
continue;
}
$leftPointer = $index + 1;
Expand All @@ -29,7 +32,7 @@ function threeSum($nums)
} else {
$listOf3Sum [] = [$number, $nums[$leftPointer], $nums[$rightPointer]];
++$leftPointer;
while (isset($nums[$leftPointer], $nums[$leftPointer - 1]) && $nums[$leftPointer] == $nums[$leftPointer - 1]) {
while (isset($nums[$leftPointer], $nums[$leftPointer - 1]) && $nums[$leftPointer] === $nums[$leftPointer - 1]) {
++$leftPointer;
}
}
Expand Down
5 changes: 4 additions & 1 deletion ArrayProblems/TwoSum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

namespace Hakam\LeetCodePhp\ArrayProblems;

/**
* LeetCode Problem Link: https://leetcode.com/problems/two-sum
*/
class TwoSum
{
/**
* @param Integer[] $nums
* @param Integer $target
* @return Integer[]
*/
function twoSum($nums, $target): array
public function twoSum(array $nums, int $target): array
{
$hashList = [];
foreach ($nums as $key => $num)
Expand Down
19 changes: 9 additions & 10 deletions LinkedList/ReOrderList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@

namespace Hakam\LeetCodePhp\LinkedList;


/**
* LeetCode Problem Link: https://leetcode.com/problems/reorder-list
*/
class ReOrderList
{
/**
* @param ListNode $head
* @return NULL
*/
function reorderList(ListNode $head) {
public function reorderList(ListNode $head)
{
$arrayList = [];
$order = new ListNode();
$copyHead = $head;
while($copyHead != null)
{
while ($copyHead !== null) {
$arrayList [] = $copyHead;
$copyHead = $copyHead->next;
}
$head = New ListNode();
$head = new ListNode();
$order = $head;
while(!empty($arrayList))
{
while (!empty($arrayList)) {
$temp = array_shift($arrayList);
$tempNext = array_pop($arrayList);
if($tempNext !== null)
{
if ($tempNext !== null) {
$tempNext->next = null;
}
$temp->next = $tempNext;
Expand Down
8 changes: 5 additions & 3 deletions LinkedList/ReverseLinkedList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

namespace Hakam\LeetCodePhp\LinkedList;

/**
* LeetCode Problem Link : https://leetcode.com/problems/reverse-linked-list
*/
class ReverseLinkedList
{
public function reverseList(ListNode $node)
public function reverseList(ListNode $node): ?ListNode
{
$current = $node;
$prev = null;
while($current!==null)
{
while ($current !== null) {
$tempNode = $current->next;
$current->next = $prev;
$prev = $current;
Expand Down
3 changes: 3 additions & 0 deletions String/FindAllAnagramsInAString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Hakam\LeetCodePhp\String;

/**
* LeetCode Problem Link : https://leetcode.com/problems/find-all-anagrams-in-a-string
*/
class FindAllAnagramsInAString
{

Expand Down
3 changes: 3 additions & 0 deletions String/RemoveAllAdjacentDuplicatesInString.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Hakam\LeetCodePhp\String;

/**
* LeetCode Problem Link: https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string
*/
class RemoveAllAdjacentDuplicatesInString
{

Expand Down
17 changes: 7 additions & 10 deletions Tree/BinaryTreeLevelOrderTraversal.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace Hakam\LeetCodePhp\Tree;

use SplQueue;

/**
* LeetCode Problem Link : https://leetcode.com/problems/binary-tree-level-order-traversal
*/
class BinaryTreeLevelOrderTraversal
{
/**
* @param TreeNode $root
* @param TreeNode|null $root
* @return Integer[][]
*/

public function levelOrder($root): array
public function levelOrder(?TreeNode $root): array
{
if($root === null)
{
Expand All @@ -20,17 +21,13 @@ public function levelOrder($root): array
$bFT = [];
$tQ = [];
$tQ [] = [$root,0];
$level = 0;

while(count($tQ) !== 0)
{
[$node,$nodeLevel] = array_shift($tQ); //level 0
$bFT[$nodeLevel][] = $node->val;
$level = $nodeLevel;
++$level;
if($node->left !== null) {$tQ[] = [$node->left,$level];}
if($node->right !== null) {$tQ[] = [$node->right,$level];}

if($node->left !== null) {$tQ[] = [$node->left,$nodeLevel+1];}
if($node->right !== null) {$tQ[] = [$node->right,$nodeLevel+1];}
}
return $bFT;
}
Expand Down
Loading

0 comments on commit f9fbaa2

Please sign in to comment.