Skip to content

Commit 8fc0210

Browse files
authored
Added tasks 230-1143
1 parent 335d058 commit 8fc0210

File tree

25 files changed

+1806
-0
lines changed

25 files changed

+1806
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 230\. Kth Smallest Element in a BST
5+
6+
Medium
7+
8+
Given the `root` of a binary search tree, and an integer `k`, return _the_ <code>k<sup>th</sup></code> _smallest value (**1-indexed**) of all the values of the nodes in the tree_.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree1.jpg)
13+
14+
**Input:** root = [3,1,4,null,2], k = 1
15+
16+
**Output:** 1
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/01/28/kthtree2.jpg)
21+
22+
**Input:** root = [5,3,6,2,4,null,null,1], k = 3
23+
24+
**Output:** 3
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the tree is `n`.
29+
* <code>1 <= k <= n <= 10<sup>4</sup></code>
30+
* <code>0 <= Node.val <= 10<sup>4</sup></code>
31+
32+
**Follow up:** If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?
33+
34+
## Solution
35+
36+
```php
37+
/**
38+
* Definition for a binary tree node.
39+
* class TreeNode {
40+
* public $val = null;
41+
* public $left = null;
42+
* public $right = null;
43+
* function __construct($val = 0, $left = null, $right = null) {
44+
* $this->val = $val;
45+
* $this->left = $left;
46+
* $this->right = $right;
47+
* }
48+
* }
49+
*/
50+
class Solution {
51+
private $k;
52+
private $count = 0;
53+
private $val;
54+
55+
/**
56+
* @param TreeNode $root
57+
* @param Integer $k
58+
* @return Integer
59+
*/
60+
public function kthSmallest($root, $k) {
61+
$this->k = $k;
62+
$this->calculate($root);
63+
return $this->val;
64+
}
65+
66+
private function calculate($node) {
67+
if ($node->left == null && $node->right == null) {
68+
$this->count++;
69+
if ($this->count == $this->k) {
70+
$this->val = $node->val;
71+
}
72+
return;
73+
}
74+
if ($node->left != null) {
75+
$this->calculate($node->left);
76+
}
77+
$this->count++;
78+
if ($this->count == $this->k) {
79+
$this->val = $node->val;
80+
return;
81+
}
82+
if ($node->right != null) {
83+
$this->calculate($node->right);
84+
}
85+
}
86+
}
87+
```
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 234\. Palindrome Linked List
5+
6+
Easy
7+
8+
Given the `head` of a singly linked list, return `true` if it is a palindrome.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2021/03/03/pal1linked-list.jpg)
13+
14+
**Input:** head = [1,2,2,1]
15+
16+
**Output:** true
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2021/03/03/pal2linked-list.jpg)
21+
22+
**Input:** head = [1,2]
23+
24+
**Output:** false
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the list is in the range <code>[1, 10<sup>5</sup>]</code>.
29+
* `0 <= Node.val <= 9`
30+
31+
**Follow up:** Could you do it in `O(n)` time and `O(1)` space?
32+
33+
## Solution
34+
35+
```php
36+
/**
37+
* Definition for a singly-linked list.
38+
* class ListNode {
39+
* public $val = 0;
40+
* public $next = null;
41+
* function __construct($val = 0, $next = null) {
42+
* $this->val = $val;
43+
* $this->next = $next;
44+
* }
45+
* }
46+
*/
47+
class Solution {
48+
/**
49+
* @param ListNode $head
50+
* @return Boolean
51+
*/
52+
public function isPalindrome($head) {
53+
$array = [];
54+
while ($head) {
55+
$array[] = $head->val;
56+
$head = $head->next;
57+
}
58+
$cnt = count($array);
59+
for ($x = 0; $x < $cnt / 2; $x++) {
60+
$yKey = $cnt - 1 - $x;
61+
62+
if ($array[$x] !== $array[$yKey]) {
63+
return false;
64+
}
65+
}
66+
return true;
67+
}
68+
}
69+
```
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 236\. Lowest Common Ancestor of a Binary Tree
5+
6+
Medium
7+
8+
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
9+
10+
According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): “The lowest common ancestor is defined between two nodes `p` and `q` as the lowest node in `T` that has both `p` and `q` as descendants (where we allow **a node to be a descendant of itself**).”
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png)
15+
16+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
17+
18+
**Output:** 3
19+
20+
**Explanation:** The LCA of nodes 5 and 1 is 3.
21+
22+
**Example 2:**
23+
24+
![](https://assets.leetcode.com/uploads/2018/12/14/binarytree.png)
25+
26+
**Input:** root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
27+
28+
**Output:** 5
29+
30+
**Explanation:** The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
31+
32+
**Example 3:**
33+
34+
**Input:** root = [1,2], p = 1, q = 2
35+
36+
**Output:** 1
37+
38+
**Constraints:**
39+
40+
* The number of nodes in the tree is in the range <code>[2, 10<sup>5</sup>]</code>.
41+
* <code>-10<sup>9</sup> <= Node.val <= 10<sup>9</sup></code>
42+
* All `Node.val` are **unique**.
43+
* `p != q`
44+
* `p` and `q` will exist in the tree.
45+
46+
## Solution
47+
48+
```php
49+
/**
50+
* Definition for a binary tree node.
51+
* class TreeNode {
52+
* public $val = null;
53+
* public $left = null;
54+
* public $right = null;
55+
* function __construct($value) { $this->val = $value; }
56+
* }
57+
*/
58+
class Solution {
59+
/**
60+
* @param TreeNode $root
61+
* @param TreeNode $p
62+
* @param TreeNode $q
63+
* @return TreeNode
64+
*/
65+
public function lowestCommonAncestor($root, $p, $q) {
66+
if ($root == null) {
67+
return null;
68+
}
69+
if ($root->val == $p->val || $root->val == $q->val) {
70+
return $root;
71+
}
72+
$left = $this->lowestCommonAncestor($root->left, $p, $q);
73+
$right = $this->lowestCommonAncestor($root->right, $p, $q);
74+
if ($left != null && $right != null) {
75+
return $root;
76+
}
77+
if ($left != null) {
78+
return $left;
79+
}
80+
return $right;
81+
}
82+
}
83+
```
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![](https://img.shields.io/github/stars/LeetCode-in-Php/LeetCode-in-Php?label=Stars&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php)
2+
[![](https://img.shields.io/github/forks/LeetCode-in-Php/LeetCode-in-Php?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/LeetCode-in-Php/LeetCode-in-Php/fork)
3+
4+
## 238\. Product of Array Except Self
5+
6+
Medium
7+
8+
Given an integer array `nums`, return _an array_ `answer` _such that_ `answer[i]` _is equal to the product of all the elements of_ `nums` _except_ `nums[i]`.
9+
10+
The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
11+
12+
You must write an algorithm that runs in `O(n)` time and without using the division operation.
13+
14+
**Example 1:**
15+
16+
**Input:** nums = [1,2,3,4]
17+
18+
**Output:** [24,12,8,6]
19+
20+
**Example 2:**
21+
22+
**Input:** nums = [-1,1,0,-3,3]
23+
24+
**Output:** [0,0,9,0,0]
25+
26+
**Constraints:**
27+
28+
* <code>2 <= nums.length <= 10<sup>5</sup></code>
29+
* `-30 <= nums[i] <= 30`
30+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
31+
32+
**Follow up:** Can you solve the problem in `O(1) `extra space complexity? (The output array **does not** count as extra space for space complexity analysis.)
33+
34+
## Solution
35+
36+
```php
37+
class Solution {
38+
/**
39+
* @param Integer[] $nums
40+
* @return Integer[]
41+
*/
42+
public function productExceptSelf($nums) {
43+
$product = 1;
44+
$ans = array_fill(0, count($nums), 0);
45+
foreach ($nums as $num) {
46+
$product = $product * $num;
47+
}
48+
for ($i = 0; $i < count($nums); $i++) {
49+
if ($nums[$i] != 0) {
50+
$ans[$i] = $product / $nums[$i];
51+
} else {
52+
$p = 1;
53+
for ($j = 0; $j < count($nums); $j++) {
54+
if ($j != $i) {
55+
$p = $p * $nums[$j];
56+
}
57+
}
58+
$ans[$i] = $p;
59+
}
60+
}
61+
return $ans;
62+
}
63+
}
64+
```

0 commit comments

Comments
 (0)