500 lines
15 KiB
Markdown
500 lines
15 KiB
Markdown
|
<!-- GFM-TOC -->
|
|||
|
* [50. 第一个只出现一次的字符位置](#50-第一个只出现一次的字符位置)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [51. 数组中的逆序对](#51-数组中的逆序对)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [52. 两个链表的第一个公共结点](#52-两个链表的第一个公共结点)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [53. 数字在排序数组中出现的次数](#53-数字在排序数组中出现的次数)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [54. 二叉查找树的第 K 个结点](#54-二叉查找树的第-k-个结点)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [55.1 二叉树的深度](#551-二叉树的深度)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [55.2 平衡二叉树](#552-平衡二叉树)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [56. 数组中只出现一次的数字](#56-数组中只出现一次的数字)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [57.1 和为 S 的两个数字](#571-和为-s-的两个数字)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [57.2 和为 S 的连续正数序列](#572-和为-s-的连续正数序列)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [58.1 翻转单词顺序列](#581-翻转单词顺序列)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [58.2 左旋转字符串](#582-左旋转字符串)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
* [59. 滑动窗口的最大值](#59-滑动窗口的最大值)
|
|||
|
* [题目描述](#题目描述)
|
|||
|
* [解题思路](#解题思路)
|
|||
|
<!-- GFM-TOC -->
|
|||
|
|
|||
|
|
|||
|
# 50. 第一个只出现一次的字符位置
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/1c82e8cf713b4bbeb2a5b31cf5b0417c?tpId=13&tqId=11187&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
在一个字符串中找到第一个只出现一次的字符,并返回它的位置。
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
最直观的解法是使用 HashMap 对出现次数进行统计,但是考虑到要统计的字符范围有限,因此可以使用整型数组代替 HashMap。
|
|||
|
|
|||
|
```java
|
|||
|
public int FirstNotRepeatingChar(String str) {
|
|||
|
int[] cnts = new int[256];
|
|||
|
for (int i = 0; i < str.length(); i++)
|
|||
|
cnts[str.charAt(i)]++;
|
|||
|
for (int i = 0; i < str.length(); i++)
|
|||
|
if (cnts[str.charAt(i)] == 1)
|
|||
|
return i;
|
|||
|
return -1;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
以上实现的空间复杂度还不是最优的。考虑到只需要找到只出现一次的字符,那么需要统计的次数信息只有 0,1,更大,使用两个比特位就能存储这些信息。
|
|||
|
|
|||
|
```java
|
|||
|
public int FirstNotRepeatingChar2(String str) {
|
|||
|
BitSet bs1 = new BitSet(256);
|
|||
|
BitSet bs2 = new BitSet(256);
|
|||
|
for (char c : str.toCharArray()) {
|
|||
|
if (!bs1.get(c) && !bs2.get(c))
|
|||
|
bs1.set(c); // 0 0 -> 0 1
|
|||
|
else if (bs1.get(c) && !bs2.get(c))
|
|||
|
bs2.set(c); // 0 1 -> 1 1
|
|||
|
}
|
|||
|
for (int i = 0; i < str.length(); i++) {
|
|||
|
char c = str.charAt(i);
|
|||
|
if (bs1.get(c) && !bs2.get(c)) // 0 1
|
|||
|
return i;
|
|||
|
}
|
|||
|
return -1;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 51. 数组中的逆序对
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/96bd6684e04a44eb80e6a68efc0ec6c5?tpId=13&tqId=11188&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
```java
|
|||
|
private long cnt = 0;
|
|||
|
private int[] tmp; // 在这里声明辅助数组,而不是在 merge() 递归函数中声明
|
|||
|
|
|||
|
public int InversePairs(int[] nums) {
|
|||
|
tmp = new int[nums.length];
|
|||
|
mergeSort(nums, 0, nums.length - 1);
|
|||
|
return (int) (cnt % 1000000007);
|
|||
|
}
|
|||
|
|
|||
|
private void mergeSort(int[] nums, int l, int h) {
|
|||
|
if (h - l < 1)
|
|||
|
return;
|
|||
|
int m = l + (h - l) / 2;
|
|||
|
mergeSort(nums, l, m);
|
|||
|
mergeSort(nums, m + 1, h);
|
|||
|
merge(nums, l, m, h);
|
|||
|
}
|
|||
|
|
|||
|
private void merge(int[] nums, int l, int m, int h) {
|
|||
|
int i = l, j = m + 1, k = l;
|
|||
|
while (i <= m || j <= h) {
|
|||
|
if (i > m)
|
|||
|
tmp[k] = nums[j++];
|
|||
|
else if (j > h)
|
|||
|
tmp[k] = nums[i++];
|
|||
|
else if (nums[i] < nums[j])
|
|||
|
tmp[k] = nums[i++];
|
|||
|
else {
|
|||
|
tmp[k] = nums[j++];
|
|||
|
this.cnt += m - i + 1; // nums[i] >= nums[j],说明 nums[i...mid] 都大于 nums[j]
|
|||
|
}
|
|||
|
k++;
|
|||
|
}
|
|||
|
for (k = l; k <= h; k++)
|
|||
|
nums[k] = tmp[k];
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 52. 两个链表的第一个公共结点
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tqId=11189&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
<div align="center"> <img src="pics/8f6f9dc9-9ecd-47c8-b50e-2814f0219056.png" width="500"/> </div><br>
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
设 A 的长度为 a + c,B 的长度为 b + c,其中 c 为尾部公共部分长度,可知 a + c + b = b + c + a。
|
|||
|
|
|||
|
当访问链表 A 的指针访问到链表尾部时,令它从链表 B 的头部重新开始访问链表 B;同样地,当访问链表 B 的指针访问到链表尾部时,令它从链表 A 的头部重新开始访问链表 A。这样就能控制访问 A 和 B 两个链表的指针能同时访问到交点。
|
|||
|
|
|||
|
```java
|
|||
|
public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {
|
|||
|
ListNode l1 = pHead1, l2 = pHead2;
|
|||
|
while (l1 != l2) {
|
|||
|
l1 = (l1 == null) ? pHead2 : l1.next;
|
|||
|
l2 = (l2 == null) ? pHead1 : l2.next;
|
|||
|
}
|
|||
|
return l1;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 53. 数字在排序数组中出现的次数
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/70610bf967994b22bb1c26f9ae901fa2?tpId=13&tqId=11190&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
```html
|
|||
|
Input:
|
|||
|
nums = 1, 2, 3, 3, 3, 3, 4, 6
|
|||
|
K = 3
|
|||
|
|
|||
|
Output:
|
|||
|
4
|
|||
|
```
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
```java
|
|||
|
public int GetNumberOfK(int[] nums, int K) {
|
|||
|
int first = binarySearch(nums, K);
|
|||
|
int last = binarySearch(nums, K + 1);
|
|||
|
return (first == nums.length || nums[first] != K) ? 0 : last - first;
|
|||
|
}
|
|||
|
|
|||
|
private int binarySearch(int[] nums, int K) {
|
|||
|
int l = 0, h = nums.length;
|
|||
|
while (l < h) {
|
|||
|
int m = l + (h - l) / 2;
|
|||
|
if (nums[m] >= K)
|
|||
|
h = m;
|
|||
|
else
|
|||
|
l = m + 1;
|
|||
|
}
|
|||
|
return l;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 54. 二叉查找树的第 K 个结点
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/ef068f602dde4d28aab2b210e859150a?tpId=13&tqId=11215&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
利用二叉查找树中序遍历有序的特点。
|
|||
|
|
|||
|
```java
|
|||
|
private TreeNode ret;
|
|||
|
private int cnt = 0;
|
|||
|
|
|||
|
public TreeNode KthNode(TreeNode pRoot, int k) {
|
|||
|
inOrder(pRoot, k);
|
|||
|
return ret;
|
|||
|
}
|
|||
|
|
|||
|
private void inOrder(TreeNode root, int k) {
|
|||
|
if (root == null || cnt >= k)
|
|||
|
return;
|
|||
|
inOrder(root.left, k);
|
|||
|
cnt++;
|
|||
|
if (cnt == k)
|
|||
|
ret = root;
|
|||
|
inOrder(root.right, k);
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 55.1 二叉树的深度
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b?tpId=13&tqId=11191&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
|
|||
|
|
|||
|
<div align="center"> <img src="pics/b29f8971-9cb8-480d-b986-0e60c2ece069.png" width="350"/> </div><br>
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
```java
|
|||
|
public int TreeDepth(TreeNode root) {
|
|||
|
return root == null ? 0 : 1 + Math.max(TreeDepth(root.left), TreeDepth(root.right));
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 55.2 平衡二叉树
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=13&tqId=11192&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
平衡二叉树左右子树高度差不超过 1。
|
|||
|
|
|||
|
<div align="center"> <img src="pics/e026c24d-00fa-4e7c-97a8-95a98cdc383a.png" width="300"/> </div><br>
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
```java
|
|||
|
private boolean isBalanced = true;
|
|||
|
|
|||
|
public boolean IsBalanced_Solution(TreeNode root) {
|
|||
|
height(root);
|
|||
|
return isBalanced;
|
|||
|
}
|
|||
|
|
|||
|
private int height(TreeNode root) {
|
|||
|
if (root == null || !isBalanced)
|
|||
|
return 0;
|
|||
|
int left = height(root.left);
|
|||
|
int right = height(root.right);
|
|||
|
if (Math.abs(left - right) > 1)
|
|||
|
isBalanced = false;
|
|||
|
return 1 + Math.max(left, right);
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 56. 数组中只出现一次的数字
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/e02fdb54d7524710a7d664d082bb7811?tpId=13&tqId=11193&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
一个整型数组里除了两个数字之外,其他的数字都出现了两次,找出这两个数。
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
两个不相等的元素在位级表示上必定会有一位存在不同,将数组的所有元素异或得到的结果为不存在重复的两个元素异或的结果。
|
|||
|
|
|||
|
diff &= -diff 得到出 diff 最右侧不为 0 的位,也就是不存在重复的两个元素在位级表示上最右侧不同的那一位,利用这一位就可以将两个元素区分开来。
|
|||
|
|
|||
|
```java
|
|||
|
public void FindNumsAppearOnce(int[] nums, int num1[], int num2[]) {
|
|||
|
int diff = 0;
|
|||
|
for (int num : nums)
|
|||
|
diff ^= num;
|
|||
|
diff &= -diff;
|
|||
|
for (int num : nums) {
|
|||
|
if ((num & diff) == 0)
|
|||
|
num1[0] ^= num;
|
|||
|
else
|
|||
|
num2[0] ^= num;
|
|||
|
}
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 57.1 和为 S 的两个数字
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b?tpId=13&tqId=11195&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
输入一个递增排序的数组和一个数字 S,在数组中查找两个数,使得他们的和正好是 S。如果有多对数字的和等于 S,输出两个数的乘积最小的。
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
使用双指针,一个指针指向元素较小的值,一个指针指向元素较大的值。指向较小元素的指针从头向尾遍历,指向较大元素的指针从尾向头遍历。
|
|||
|
|
|||
|
- 如果两个指针指向元素的和 sum == target,那么得到要求的结果;
|
|||
|
- 如果 sum > target,移动较大的元素,使 sum 变小一些;
|
|||
|
- 如果 sum < target,移动较小的元素,使 sum 变大一些。
|
|||
|
|
|||
|
```java
|
|||
|
public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {
|
|||
|
int i = 0, j = array.length - 1;
|
|||
|
while (i < j) {
|
|||
|
int cur = array[i] + array[j];
|
|||
|
if (cur == sum)
|
|||
|
return new ArrayList<>(Arrays.asList(array[i], array[j]));
|
|||
|
if (cur < sum)
|
|||
|
i++;
|
|||
|
else
|
|||
|
j--;
|
|||
|
}
|
|||
|
return new ArrayList<>();
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 57.2 和为 S 的连续正数序列
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/c451a3fd84b64cb19485dad758a55ebe?tpId=13&tqId=11194&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
输出所有和为 S 的连续正数序列。
|
|||
|
|
|||
|
例如和为 100 的连续序列有:
|
|||
|
|
|||
|
```
|
|||
|
[9, 10, 11, 12, 13, 14, 15, 16]
|
|||
|
[18, 19, 20, 21, 22]。
|
|||
|
```
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
```java
|
|||
|
public ArrayList<ArrayList<Integer>> FindContinuousSequence(int sum) {
|
|||
|
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
|
|||
|
int start = 1, end = 2;
|
|||
|
int curSum = 3;
|
|||
|
while (end < sum) {
|
|||
|
if (curSum > sum) {
|
|||
|
curSum -= start;
|
|||
|
start++;
|
|||
|
} else if (curSum < sum) {
|
|||
|
end++;
|
|||
|
curSum += end;
|
|||
|
} else {
|
|||
|
ArrayList<Integer> list = new ArrayList<>();
|
|||
|
for (int i = start; i <= end; i++)
|
|||
|
list.add(i);
|
|||
|
ret.add(list);
|
|||
|
curSum -= start;
|
|||
|
start++;
|
|||
|
end++;
|
|||
|
curSum += end;
|
|||
|
}
|
|||
|
}
|
|||
|
return ret;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 58.1 翻转单词顺序列
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/3194a4f4cf814f63919d0790578d51f3?tpId=13&tqId=11197&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
```html
|
|||
|
Input:
|
|||
|
"I am a student."
|
|||
|
|
|||
|
Output:
|
|||
|
"student. a am I"
|
|||
|
```
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
题目应该有一个隐含条件,就是不能用额外的空间。虽然 Java 的题目输入参数为 String 类型,需要先创建一个字符数组使得空间复杂度为 O(N),但是正确的参数类型应该和原书一样,为字符数组,并且只能使用该字符数组的空间。任何使用了额外空间的解法在面试时都会大打折扣,包括递归解法。
|
|||
|
|
|||
|
正确的解法应该是和书上一样,先旋转每个单词,再旋转整个字符串。
|
|||
|
|
|||
|
```java
|
|||
|
public String ReverseSentence(String str) {
|
|||
|
int n = str.length();
|
|||
|
char[] chars = str.toCharArray();
|
|||
|
int i = 0, j = 0;
|
|||
|
while (j <= n) {
|
|||
|
if (j == n || chars[j] == ' ') {
|
|||
|
reverse(chars, i, j - 1);
|
|||
|
i = j + 1;
|
|||
|
}
|
|||
|
j++;
|
|||
|
}
|
|||
|
reverse(chars, 0, n - 1);
|
|||
|
return new String(chars);
|
|||
|
}
|
|||
|
|
|||
|
private void reverse(char[] c, int i, int j) {
|
|||
|
while (i < j)
|
|||
|
swap(c, i++, j--);
|
|||
|
}
|
|||
|
|
|||
|
private void swap(char[] c, int i, int j) {
|
|||
|
char t = c[i];
|
|||
|
c[i] = c[j];
|
|||
|
c[j] = t;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 58.2 左旋转字符串
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/12d959b108cb42b1ab72cef4d36af5ec?tpId=13&tqId=11196&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
```html
|
|||
|
Input:
|
|||
|
S="abcXYZdef"
|
|||
|
K=3
|
|||
|
|
|||
|
Output:
|
|||
|
"XYZdefabc"
|
|||
|
```
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
先将 "abc" 和 "XYZdef" 分别翻转,得到 "cbafedZYX",然后再把整个字符串翻转得到 "XYZdefabc"。
|
|||
|
|
|||
|
```java
|
|||
|
public String LeftRotateString(String str, int n) {
|
|||
|
if (n >= str.length())
|
|||
|
return str;
|
|||
|
char[] chars = str.toCharArray();
|
|||
|
reverse(chars, 0, n - 1);
|
|||
|
reverse(chars, n, chars.length - 1);
|
|||
|
reverse(chars, 0, chars.length - 1);
|
|||
|
return new String(chars);
|
|||
|
}
|
|||
|
|
|||
|
private void reverse(char[] chars, int i, int j) {
|
|||
|
while (i < j)
|
|||
|
swap(chars, i++, j--);
|
|||
|
}
|
|||
|
|
|||
|
private void swap(char[] chars, int i, int j) {
|
|||
|
char t = chars[i];
|
|||
|
chars[i] = chars[j];
|
|||
|
chars[j] = t;
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
# 59. 滑动窗口的最大值
|
|||
|
|
|||
|
[NowCoder](https://www.nowcoder.com/practice/1624bc35a45c42c0bc17d17fa0cba788?tpId=13&tqId=11217&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
|||
|
|
|||
|
## 题目描述
|
|||
|
|
|||
|
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。
|
|||
|
|
|||
|
例如,如果输入数组 {2, 3, 4, 2, 6, 2, 5, 1} 及滑动窗口的大小 3,那么一共存在 6 个滑动窗口,他们的最大值分别为 {4, 4, 6, 6, 6, 5}。
|
|||
|
|
|||
|
## 解题思路
|
|||
|
|
|||
|
```java
|
|||
|
public ArrayList<Integer> maxInWindows(int[] num, int size) {
|
|||
|
ArrayList<Integer> ret = new ArrayList<>();
|
|||
|
if (size > num.length || size < 1)
|
|||
|
return ret;
|
|||
|
PriorityQueue<Integer> heap = new PriorityQueue<>((o1, o2) -> o2 - o1); /* 大顶堆 */
|
|||
|
for (int i = 0; i < size; i++)
|
|||
|
heap.add(num[i]);
|
|||
|
ret.add(heap.peek());
|
|||
|
for (int i = 0, j = i + size; j < num.length; i++, j++) { /* 维护一个大小为 size 的大顶堆 */
|
|||
|
heap.remove(num[i]);
|
|||
|
heap.add(num[j]);
|
|||
|
ret.add(heap.peek());
|
|||
|
}
|
|||
|
return ret;
|
|||
|
}
|
|||
|
```
|