File tree 4 files changed +161
-0
lines changed
4 files changed +161
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for binary tree
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+
11
+ public class BSTIterator {
12
+
13
+ Stack <TreeNode > stack ;
14
+ public BSTIterator (TreeNode root ) {
15
+ stack = new Stack <>();
16
+ TreeNode curr = root ;
17
+ while (curr != null ) {
18
+ stack .push (curr );
19
+ curr = curr .left ;
20
+ }
21
+ }
22
+
23
+ /** @return whether we have a next smallest number */
24
+ public boolean hasNext () {
25
+ return !stack .empty ();
26
+ }
27
+
28
+ /** @return the next smallest number */
29
+ public int next () {
30
+ TreeNode node = stack .pop ();
31
+ TreeNode curr = node ;
32
+
33
+ if (curr .right != null ) {
34
+ curr = curr .right ;
35
+ while (curr != null ) {
36
+ stack .push (curr );
37
+ if (curr .left != null ) {
38
+ curr = curr .left ;
39
+ }
40
+ else {
41
+ break ;
42
+ }
43
+ }
44
+ }
45
+
46
+ return node .val ;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Your BSTIterator will be called like this:
52
+ * BSTIterator i = new BSTIterator(root);
53
+ * while (i.hasNext()) v[f()] = i.next();
54
+ */
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public static boolean isNStraightHand (int [] hand , int W ) {
3
+ if (hand .length %W != 0 ) {
4
+ return false ;
5
+ }
6
+
7
+ Map <Integer , Integer > map = new TreeMap <>();
8
+
9
+ for (int num : hand ) {
10
+ map .put (num , map .getOrDefault (num , 0 )+1 );
11
+ }
12
+
13
+ Set <Integer > keys ;
14
+ int batches = hand .length /W ;
15
+ int batchSize ;
16
+
17
+ while (batches -- > 0 ) {
18
+ keys = map .keySet ();
19
+ Iterator <Integer > iterator = keys .iterator ();
20
+ batchSize = W ;
21
+ int val = iterator .next ();
22
+
23
+ while (batchSize -- > 0 ) {
24
+ if (map .containsKey (val )) {
25
+ if (map .get (val ) == 1 ) {
26
+ map .remove (val );
27
+ }
28
+ else {
29
+ map .put (val , map .getOrDefault (val , 0 )-1 );
30
+ }
31
+ val ++;
32
+ }
33
+ else {
34
+ return false ;
35
+ }
36
+ }
37
+ }
38
+
39
+ return true ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public boolean isSubsequence (String s , String t ) {
3
+ char [] sChar = s .toCharArray ();
4
+ char [] tChar = t .toCharArray ();
5
+
6
+ int i =0 ;
7
+ int j =0 ;
8
+ int low = s .length ();
9
+ int high = t .length ();
10
+
11
+ while (i <low && j <high ) {
12
+ if (sChar [i ] == tChar [j ]) {
13
+ i ++;
14
+ }
15
+
16
+ j ++;
17
+ }
18
+
19
+ return i == low ;
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int numMatchingSubseq (String S , String [] words ) {
3
+ int count = 0 ;
4
+ Set <String > match = new HashSet <>();
5
+ Set <String > unmatch = new HashSet <>();
6
+
7
+ for (String word : words ) {
8
+ if (match .contains (word )) {
9
+ count ++;
10
+ }
11
+ else if (unmatch .contains (word )) {
12
+ continue ;
13
+ }
14
+ else if (isSubsequence (word , S )) {
15
+ match .add (word );
16
+ count ++;
17
+ }
18
+ else {
19
+ unmatch .add (word );
20
+ }
21
+ }
22
+
23
+ return count ;
24
+ }
25
+
26
+ private boolean isSubsequence (String s , String t ) {
27
+ char [] sChar = s .toCharArray ();
28
+ char [] tChar = t .toCharArray ();
29
+
30
+ int i =0 ;
31
+ int j =0 ;
32
+ int low = s .length ();
33
+ int high = t .length ();
34
+
35
+ while (i <low && j <high ) {
36
+ if (sChar [i ] == tChar [j ]) {
37
+ i ++;
38
+ }
39
+
40
+ j ++;
41
+ }
42
+
43
+ return i == low ;
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments