File tree 4 files changed +152
-1
lines changed
binary-trees/lowest-common-ancestor
implement-queue-using-stacks
4 files changed +152
-1
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -46,7 +46,6 @@ function stringMatching($s, $t) {
46
46
47
47
$ s = sortStr ($ s );
48
48
$ t = sortStr ($ t );
49
- str_replace()
50
49
51
50
for ($ i = 0 ; $ i <= ($ m - $ n ); $ i ++) {
52
51
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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 );
You can’t perform that action at this time.
0 commit comments