http://www.szeju.com/index.php/other/35241410.html
https://xuezhashuati.blogspot.com/2017/04/lintcode-625-partition-array-ii.html
[LintCode] 625 Partition Array II
Partition an unsorted integer array into three parts:
The front part < low
The middle part >= low & <= high
The tail part > high
Return any of the possible solutions.
Notice
low <= high in all testcases.
Example
Given [4,3,4,1,2,3,1,2], and low = 2 and high = 3.
Change to [1,1,2,3,2,3,4,4].
([1,1,2,2,3,3,4,4] is also a correct answer, but [1,2,1,2,3,3,4,4] is not)
Challenge
Do it in place.
Do it in one pass (one loop).
思路
标准的3-way-partition。判断的时候不是只有一个v,而是有low和high而已。
[LintCode] 606 Kth Largest Element II
Find K-th largest element in an array. and N is much larger than k.
Notice
You can swap elements in the array
Example
In array [9,3,2,4,8], the 3rd largest element is 4.
In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4, 3rd largest element is 3 and etc.
思路
建立一个min-heap。过一遍数组把每个数字往里面扔。如果超过k个,就扔出来一个。
最后里面剩下k个最大的。头上这个就是第k大。
596、Minimum Subtree
- 题目
Given a binary tree, find the subtree with minimum sum. Return the root of the subtree.
注意事项
LintCode will print the subtree which root is your return node.
It’s guaranteed that there is only one subtree with minimum sum and the given binary tree is not an empty tree.
样例
Given a binary tree:
…. 1
… / \
. -5 2
. / \ / \
0 2 -4 -5
return the node 1.
597、Subtree with Maximum Average
- 题目
Given a binary tree, find the subtree with maximum average. Return the root of the subtree.
【再来遍~】
注意事项
LintCode will print the subtree which root is your return node.
It’s guaranteed that there is only one subtree with maximum average.
样例
Given a binary tree:
….. 1
…. / \
. -5 11
. / \ / \
1 2 4 -2
return the node 11.
453、将二叉树拆成链表
- 题目
将一棵二叉树按照前序遍历拆解成为一个假链表。所谓的假链表是说,用二叉树的 right 指针,来表示链表中的 next 指针。
【再来遍~】
注意事项
不要忘记将左儿子标记为 null,否则你可能会得到空间溢出或是时间溢出。
样例
…. … 1
….. …. \
1 ……. 2
/ \ …… \
2 5 => 3
/ \ \ … … \
3 4 6 …… 4
……….. .. … \
……….. … … 5
………… … … \
……….. . … … 6
https://xuezhashuati.blogspot.com/2017/04/lintcode-625-partition-array-ii.html
[LintCode] 625 Partition Array II
Partition an unsorted integer array into three parts:
The front part < low
The middle part >= low & <= high
The tail part > high
Return any of the possible solutions.
Notice
low <= high in all testcases.
Example
Given [4,3,4,1,2,3,1,2], and low = 2 and high = 3.
Change to [1,1,2,3,2,3,4,4].
([1,1,2,2,3,3,4,4] is also a correct answer, but [1,2,1,2,3,3,4,4] is not)
Challenge
Do it in place.
Do it in one pass (one loop).
思路
标准的3-way-partition。判断的时候不是只有一个v,而是有low和high而已。
public void partition2(int[] nums, int low, int high) { // Write your code here int lt = 0; int gt = nums.length - 1; int i = 0; while (i <= gt) { if (nums[i] < low) { exch(nums, lt++, i++); } else if (nums[i] > high) { exch(nums, i, gt--); } else { i++; } } } public void exch(int[] nums, int x, int y) { int tmp = nums[x]; nums[x] = nums[y]; nums[y] = tmp; }https://xuezhashuati.blogspot.com/2017/04/lintcode-606-kth-largest-element-ii.html
[LintCode] 606 Kth Largest Element II
Find K-th largest element in an array. and N is much larger than k.
Notice
You can swap elements in the array
Example
In array [9,3,2,4,8], the 3rd largest element is 4.
In array [1,2,3,4,5], the 1st largest element is 5, 2nd largest element is 4, 3rd largest element is 3 and etc.
思路
建立一个min-heap。过一遍数组把每个数字往里面扔。如果超过k个,就扔出来一个。
最后里面剩下k个最大的。头上这个就是第k大。
public int kthLargestElement2(int[] nums, int k) { // Write your code here PriorityQueue<Integer> pq = new PriorityQueue<Integer>(k); for (int num : nums) { pq.add(num); if (pq.size() > k) { pq.poll(); } } return pq.poll(); }