File tree 2 files changed +75
-0
lines changed
0173.Binary Search Tree Iterator
2 files changed +75
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * public class TreeNode {
4
+ * int val;
5
+ * TreeNode left;
6
+ * TreeNode right;
7
+ * TreeNode(int x) { val = x; }
8
+ * }
9
+ */
10
+ class BSTIterator {
11
+
12
+ Stack <TreeNode > vector = new Stack <>();
13
+ TreeNode current ;
14
+
15
+ public BSTIterator (TreeNode root ) {
16
+ current = root ;
17
+ // 一直放入左儿子(左)
18
+ while (current != null ) {
19
+ vector .push (current );
20
+ current = current .left ;
21
+ }
22
+ }
23
+
24
+ /** @return whether we have a next smallest number */
25
+ public boolean hasNext () {
26
+ return !vector .isEmpty () || current != null ;
27
+ }
28
+
29
+ /** @return the next smallest number */
30
+ public int next () {
31
+ // 一直放入左儿子(左)
32
+ while (current != null ) {
33
+ vector .push (current );
34
+ current = current .left ;
35
+ }
36
+ int ans = 0 ;
37
+ // 访问当前元素(中),把右儿子入栈(右)
38
+ if (!vector .isEmpty ()) {
39
+ current = vector .pop ();
40
+ ans = current .val ;
41
+ current = current .right ;
42
+ }
43
+ return ans ;
44
+ }
45
+ }
46
+
47
+ /**
48
+ * Your BSTIterator object will be instantiated and called as such:
49
+ * BSTIterator obj = new BSTIterator(root);
50
+ * int param_1 = obj.next();
51
+ * boolean param_2 = obj.hasNext();
52
+ */
Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public String largestNumber (int [] nums ) {
3
+
4
+ String [] strs = new String [nums .length ];
5
+
6
+ for (int i = 0 ; i < strs .length ; i ++) {
7
+ strs [i ] = nums [i ] + "" ;
8
+ }
9
+
10
+ Arrays .sort (strs , new Comparator <String >() {
11
+
12
+ public int compare (String x , String y ) {
13
+ return (y + x ).compareTo (x + y );
14
+ }
15
+ });
16
+
17
+ if ("0" .equals (strs [0 ])) {
18
+ return "0" ;
19
+ }
20
+
21
+ return String .join ("" , strs );
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments