File tree 4 files changed +145
-0
lines changed 4 files changed +145
-0
lines changed Original file line number Diff line number Diff line change
1
+ package org .sean .array ;
2
+
3
+ import java .util .*;
4
+
5
+ /** * 22. Generate Parentheses */
6
+ public class ParenthesesGenerator {
7
+ private static final String pair = "()" ;
8
+ private Set <String > set = new HashSet <>();
9
+
10
+ public List <String > generateParenthesis (int n ) {
11
+ if (n == 0 ) return Collections .emptyList ();
12
+
13
+ if (n == 1 ) return Arrays .asList (pair );
14
+
15
+ return reduce (pair , generateParenthesis (n - 1 ));
16
+ }
17
+
18
+ private List <String > reduce (String s , List <String > strings ) {
19
+ int size = strings .size ();
20
+
21
+ set .clear ();
22
+ for (int i = 0 ; i < size ; i ++) { // e.g [ ()(), (()) ]
23
+ String elem = strings .get (i );
24
+
25
+ set .add (elem + s );
26
+
27
+ int len = elem .length ();
28
+ for (int j = 0 ; j < len ; j ++) {
29
+ set .add (elem .substring (0 , j ) + s + elem .substring (j ));
30
+ }
31
+ }
32
+
33
+ return new LinkedList <>(set );
34
+ }
35
+ }
Original file line number Diff line number Diff line change
1
+ package org .sean .array ;
2
+
3
+ /** * 283 Move Zeroes */
4
+ public class ZeroesMover {
5
+ private void swap (int [] array , int i , int j ) {
6
+ int tmp = array [i ];
7
+ array [i ] = array [j ];
8
+ array [j ] = tmp ;
9
+ }
10
+
11
+ public void moveZeroes (int [] nums ) {
12
+ if (nums != null || nums .length > 0 ) {
13
+ int zeroCnt = 0 ;
14
+ for (int i = 0 ; i < nums .length ; i ++) {
15
+ if (nums [i ] != 0 ) {
16
+ int p = i - 1 ;
17
+ while (p >= 0 ) {
18
+ if (nums [p ] != 0 ) {
19
+ break ;
20
+ }
21
+
22
+ --p ;
23
+ }
24
+
25
+ if ((p + 1 ) >= 0 && (p + 1 ) != i ) {
26
+ swap (nums , p + 1 , i );
27
+ }
28
+ }
29
+ }
30
+ }
31
+ }
32
+ }
Original file line number Diff line number Diff line change
1
+ package org .sean .tree ;
2
+
3
+ /** * 669. Trim a Binary Search Tree */
4
+ public class BinarySearchTreeTrimmer {
5
+ private TreeNode findNextNode (TreeNode node , int L , int R ) {
6
+ if (node == null ) {
7
+ return null ;
8
+ }
9
+
10
+ if (node .val >= L && node .val <= R ) return node ;
11
+
12
+ if (node .val < L ) {
13
+ return findNextNode (node .right , L , R );
14
+ } else {
15
+ return findNextNode (node .left , L , R );
16
+ }
17
+ }
18
+
19
+ public TreeNode trimBST (TreeNode root , int L , int R ) {
20
+ root = findNextNode (root , L , R );
21
+
22
+ if (root != null ) root .left = trimBST (root .left , L , R );
23
+ if (root != null ) root .right = trimBST (root .right , L , R );
24
+
25
+ return root ;
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ package org .sean .tree ;
2
+
3
+ // 951 Flip Equivalent Binary Trees
4
+ public class FlipEquivalentTree {
5
+
6
+ private boolean areNodesEqual (TreeNode t1 , TreeNode t2 ) {
7
+ if (t1 == null && t2 == null ) return true ;
8
+ else if (t1 == null || t2 == null ) return false ;
9
+ else return t1 .val == t2 .val ;
10
+ }
11
+
12
+ private void flip (TreeNode node ) {
13
+ if (node != null ) {
14
+ TreeNode tmp = node .left ;
15
+ node .left = node .right ;
16
+ node .right = tmp ;
17
+ }
18
+ }
19
+
20
+ private boolean isLeaf (TreeNode node ) {
21
+ if (node != null ) {
22
+ return node .left == null && node .right == null ;
23
+ }
24
+ return false ;
25
+ }
26
+
27
+ public boolean flipEquiv (TreeNode root1 , TreeNode root2 ) {
28
+ if (root1 == null && root2 == null ) {
29
+ return true ;
30
+ } else if (root1 == null || root2 == null ) {
31
+ return false ;
32
+ } else {
33
+ if (root1 .val == root2 .val ) {
34
+ if (isLeaf (root1 ) && isLeaf (root2 )) {
35
+ return true ;
36
+ } else {
37
+ if (!areNodesEqual (root1 .left , root2 .left )) {
38
+ flip (root1 );
39
+ }
40
+
41
+ boolean leftResult = flipEquiv (root1 .left , root2 .left );
42
+ boolean rightResult = flipEquiv (root1 .right , root2 .right );
43
+
44
+ return leftResult && rightResult ;
45
+ }
46
+ } else {
47
+ return false ;
48
+ }
49
+ }
50
+ }
51
+ }
You can’t perform that action at this time.
0 commit comments