Skip to content

Commit e4d0ab9

Browse files
committed
BST
1 parent 6506bbe commit e4d0ab9

File tree

4 files changed

+152
-1
lines changed

4 files changed

+152
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
class Solution {
4+
/**
5+
* @param TreeNode $root
6+
* @param TreeNode $p
7+
* @param TreeNode $q
8+
* @return TreeNode
9+
*/
10+
function lowestCommonAncestor($root, $p, $q) {
11+
if(!$root) return null;
12+
13+
if($root->val == $p->val || $root->val == $q->val) {
14+
return $root;
15+
}
16+
17+
$left = $this->lowestCommonAncestor($root->left, $p, $q);
18+
$right = $this->lowestCommonAncestor($root->right, $p, $q);
19+
20+
if(!$left) return $right;
21+
if(!$right) return $left;
22+
return $root;
23+
}
24+
}

leetcode/heap-sort/sort-an-array/adaa-mgbede.php

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ function stringMatching($s, $t) {
4646

4747
$s = sortStr($s);
4848
$t = sortStr($t);
49-
str_replace()
5049

5150
for($i = 0; $i <= ($m - $n); $i++) {
5251

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
class MyQueue {
4+
public $front = -1;
5+
public $rear = -1;
6+
public $arr = [];
7+
8+
/**
9+
* Initialize your data structure here.
10+
*/
11+
function __construct() {
12+
}
13+
14+
/**
15+
* Push element x to the back of queue.
16+
* @param Integer $x
17+
* @return NULL
18+
*/
19+
function push(int $x) {
20+
if ($this->empty()) {
21+
$this->front = $this->rear = 0;
22+
} else {
23+
$this->rear = $this->rear + 1;
24+
}
25+
$this->arr[$this->rear] = $x;
26+
return null;
27+
}
28+
29+
/**
30+
* Removes the element from in front of queue and returns that element.
31+
* @return Integer
32+
*/
33+
function pop(): int
34+
{
35+
$temp = $this->front;
36+
if ($this->front == $this->rear) {
37+
$this->front = $this->rear = -1;
38+
} else {
39+
$this->front = $this->front + 1;
40+
}
41+
42+
return $this->arr[$temp];
43+
}
44+
45+
/**
46+
* Get the front element.
47+
* @return Integer
48+
*/
49+
function peek(): int
50+
{
51+
return $this->arr[$this->front];
52+
}
53+
54+
/**
55+
* Returns whether the queue is empty.
56+
* @return Boolean
57+
*/
58+
function empty(): bool
59+
{
60+
if ($this->front == -1 && $this->rear == -1) {
61+
return true;
62+
}
63+
return false;
64+
}
65+
}
66+
67+
$obj = new MyQueue();
68+
$obj->push(1);
69+
$obj->pop();
70+
$obj->push(2);
71+
//$obj->peek();
72+
print_r($obj->peek()); exit;
73+
74+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
class SolutionOQ {
4+
5+
public $front = 0;
6+
public $rear = -1;
7+
public $arr = [];
8+
9+
/**
10+
* Push element x to the back of queue.
11+
* @param String $x
12+
* @return NULL
13+
*/
14+
function push(string $x) {
15+
$this->rear += 1;
16+
$this->arr[$this->rear] = $x;
17+
}
18+
19+
/**
20+
* Removes the element from in front of queue and returns that element.
21+
*/
22+
function pop()
23+
{
24+
$temp = $this->front;
25+
$this->front += 1;
26+
return $this->arr[$temp];
27+
}
28+
29+
/**
30+
* @param String $S
31+
* @param Integer $K
32+
* @return String
33+
*/
34+
function orderlyQueue(string $S, int $K): string
35+
{
36+
if ($S == "") return "";
37+
38+
$this->arr = str_split($S);
39+
$this->rear = count($this->arr) - 1;
40+
41+
for ($i = 0; $i <= $K; $i++) {
42+
$x = $this->pop();
43+
$this->push($x);
44+
}
45+
$result = "";
46+
for ($i = $this->front; $i <= $this->rear; $i++) {
47+
$result .= $this->arr[$i];
48+
}
49+
return $result;
50+
}
51+
}
52+
53+
$obj = new SolutionOQ();
54+
echo $obj->orderlyQueue("cba", 1);

0 commit comments

Comments
 (0)