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
32 changed files
with
1,096 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace leetcode\com_github_leetcode; | ||
|
||
class TreeNode { | ||
public $val; | ||
public $left; | ||
public $right; | ||
|
||
function __construct($val, $left = null, $right = null) { | ||
$this->val = $val; | ||
$this->left = $left; | ||
$this->right = $right; | ||
} | ||
|
||
public static function create($treeValues) { | ||
$root = empty($treeValues) ? null : new TreeNode($treeValues[0]); | ||
$queue = array(); | ||
array_push($queue, $root); | ||
$i = 1; | ||
while ($i < count($treeValues)) { | ||
$curr = array_shift($queue); | ||
if ($treeValues[$i] != null) { | ||
$curr->left = new TreeNode($treeValues[$i]); | ||
array_push($queue, $curr->left); | ||
} | ||
if (++$i < count($treeValues) && $treeValues[$i] != null) { | ||
$curr->right = new TreeNode($treeValues[$i]); | ||
array_push($queue, $curr->right); | ||
} | ||
$i++; | ||
} | ||
return $root; | ||
} | ||
|
||
public function __toString() { | ||
if ($this->left == null && $this->right == null) { | ||
return "" . $this->val; | ||
} else { | ||
$root = "" . $this->val; | ||
$leftValue = "null"; | ||
$rightValue = "null"; | ||
if ($this->left != null) { | ||
$leftValue = $this->left->__toString(); | ||
} | ||
if ($this->right != null) { | ||
$rightValue = $this->right->__toString(); | ||
} | ||
return $root . "," . $leftValue . "," . $rightValue; | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/php/g0001_0100/s0073_set_matrix_zeroes/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,63 @@ | ||
<?php | ||
|
||
namespace leetcode\g0001_0100\s0073_set_matrix_zeroes; | ||
|
||
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Matrix | ||
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(m*n)_Space_O(1) | ||
// #2023_12_10_Time_37_ms_(61.76%)_Space_23.4_MB_(44.12%) | ||
|
||
class Solution { | ||
/** | ||
* @param Integer[][] $matrix | ||
* @return NULL | ||
*/ | ||
public function setZeroes(&$matrix) { | ||
$m = count($matrix); | ||
$n = count($matrix[0]); | ||
$row0 = false; | ||
$col0 = false; | ||
// Check if 0th col needs to be market all 0s in future | ||
foreach ($matrix as $ints) { | ||
if ($ints[0] == 0) { | ||
$col0 = true; | ||
break; | ||
} | ||
} | ||
// Check if 0th row needs to be market all 0s in future | ||
for ($i = 0; $i < $n; $i++) { | ||
if ($matrix[0][$i] == 0) { | ||
$row0 = true; | ||
break; | ||
} | ||
} | ||
// Store the signals in 0th row and column | ||
for ($i = 1; $i < $m; $i++) { | ||
for ($j = 1; $j < $n; $j++) { | ||
if ($matrix[$i][$j] == 0) { | ||
$matrix[$i][0] = 0; | ||
$matrix[0][$j] = 0; | ||
} | ||
} | ||
} | ||
// Mark 0 for all cells based on signal from 0th row and 0th column | ||
for ($i = 1; $i < $m; $i++) { | ||
for ($j = 1; $j < $n; $j++) { | ||
if ($matrix[$i][0] == 0 || $matrix[0][$j] == 0) { | ||
$matrix[$i][$j] = 0; | ||
} | ||
} | ||
} | ||
// Set 0th column | ||
for ($i = 0; $i < $m; $i++) { | ||
if ($col0) { | ||
$matrix[$i][0] = 0; | ||
} | ||
} | ||
// Set 0th row | ||
for ($i = 0; $i < $n; $i++) { | ||
if ($row0) { | ||
$matrix[0][$i] = 0; | ||
} | ||
} | ||
} | ||
} |
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,36 @@ | ||
73\. Set Matrix Zeroes | ||
|
||
Medium | ||
|
||
Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`'s, and return _the matrix_. | ||
|
||
You must do it [in place](https://en.wikipedia.org/wiki/In-place_algorithm). | ||
|
||
**Example 1:** | ||
|
||
![](https://assets.leetcode.com/uploads/2020/08/17/mat1.jpg) | ||
|
||
**Input:** matrix = [[1,1,1],[1,0,1],[1,1,1]] | ||
|
||
**Output:** [[1,0,1],[0,0,0],[1,0,1]] | ||
|
||
**Example 2:** | ||
|
||
![](https://assets.leetcode.com/uploads/2020/08/17/mat2.jpg) | ||
|
||
**Input:** matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] | ||
|
||
**Output:** [[0,0,0,0],[0,4,5,0],[0,3,1,0]] | ||
|
||
**Constraints:** | ||
|
||
* `m == matrix.length` | ||
* `n == matrix[0].length` | ||
* `1 <= m, n <= 200` | ||
* <code>-2<sup>31</sup> <= matrix[i][j] <= 2<sup>31</sup> - 1</code> | ||
|
||
**Follow up:** | ||
|
||
* A straightforward solution using `O(mn)` space is probably a bad idea. | ||
* A simple improvement uses `O(m + n)` space, but still not the best solution. | ||
* Could you devise a constant space solution? |
35 changes: 35 additions & 0 deletions
35
src/main/php/g0001_0100/s0074_search_a_2d_matrix/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,35 @@ | ||
<?php | ||
|
||
namespace leetcode\g0001_0100\s0074_search_a_2d_matrix; | ||
|
||
// #Medium #Top_100_Liked_Questions #Array #Binary_Search #Matrix #Data_Structure_I_Day_5_Array | ||
// #Algorithm_II_Day_1_Binary_Search #Binary_Search_I_Day_8 #Level_2_Day_8_Binary_Search | ||
// #Udemy_2D_Arrays/Matrix #Big_O_Time_O(endRow+endCol)_Space_O(1) | ||
// #2023_12_10_Time_9_ms_(68.18%)_Space_19.8_MB_(51.82%) | ||
|
||
class Solution { | ||
/** | ||
* @param Integer[][] $matrix | ||
* @param Integer $target | ||
* @return Boolean | ||
*/ | ||
public function searchMatrix($matrix, $target) { | ||
$endRow = count($matrix); | ||
$endCol = count($matrix[0]); | ||
$targetRow = 0; | ||
$result = false; | ||
for ($i = 0; $i < $endRow; $i++) { | ||
if ($matrix[$i][$endCol - 1] >= $target) { | ||
$targetRow = $i; | ||
break; | ||
} | ||
} | ||
for ($i = 0; $i < $endCol; $i++) { | ||
if ($matrix[$targetRow][$i] == $target) { | ||
$result = true; | ||
break; | ||
} | ||
} | ||
return $result; | ||
} | ||
} |
Oops, something went wrong.