File tree Expand file tree Collapse file tree 3 files changed +2
-21
lines changed
solution/src/main/java/com/inuker/solution
test/src/main/java/com/inuker/test Expand file tree Collapse file tree 3 files changed +2
-21
lines changed Original file line number Diff line number Diff line change 13
13
14
14
/**
15
15
* 有三种解法:
16
- * 1,中序完整遍历一遍二叉树形成一个list,再找到最接近target的node,然后依次往左右两边延伸到找满k个节点,复杂度O(n + lgn + k)
17
16
* 2,中序遍历两遍二叉树,不过每次都会中途退出,构成双栈,一个是大于target,一个是小于target,然后依次出栈满k为止,复杂度O(n + k)
18
17
* 3,中序完整遍历一遍二叉树,依次将节点加入优先级队列,按与target的差排序,选前k个,复杂度O(nlgk)
19
18
*/
Original file line number Diff line number Diff line change 17
17
public class InorderSuccessorInBST {
18
18
19
19
// 耗时10ms
20
+ // 时间复杂度O(n)
20
21
public TreeNode inorderSuccessor (TreeNode root , TreeNode p ) {
21
22
Stack <TreeNode > stack = new Stack <TreeNode >();
22
23
TreeNode node = null , prev = null ;
Original file line number Diff line number Diff line change 1
1
package com .inuker .test ;
2
2
3
+ import com .leetcode .library .Interval ;
3
4
import com .leetcode .library .TreeNode ;
4
5
5
6
import java .util .ArrayList ;
21
22
public class main {
22
23
23
24
public static void main (String [] args ) {
24
- }
25
25
26
- public int [] maxSlidingWindow (int [] nums , int k ) {
27
- if (nums .length == 0 || k == 0 ) {
28
- return new int [0 ];
29
- }
30
- int [] result = new int [nums .length - k + 1 ];
31
- Deque <Integer > queue = new LinkedList <>();
32
- for (int i = 0 ; i < nums .length ; i ++) {
33
- while (!queue .isEmpty () && nums [i ] > nums [queue .peekLast ()]) {
34
- queue .pollLast ();
35
- }
36
- queue .offerLast (i );
37
- if (queue .peekFirst () + k <= i ) {
38
- queue .pollFirst ();
39
- }
40
- if (i - k + 1 >= 0 ) {
41
- result [i - k + 1 ] = nums [queue .peekFirst ()];
42
- }
43
- }
44
- return result ;
45
26
}
46
27
}
You can’t perform that action at this time.
0 commit comments