前言
美团算法合集,呃呃
到店
8. 字符串转换整数 (atoi) - 力扣(LeetCode)
56. 合并区间 - 力扣(LeetCode)
40. 组合总和 II - 力扣(LeetCode)
20. 有效的括号 - 力扣(LeetCode)
692. 前K个高频单词 - 力扣(LeetCode)
直播
215. 数组中的第K个最大元素 - 力扣(LeetCode)
143. 重排链表 - 力扣(LeetCode)
206. 反转链表 - 力扣(LeetCode)
到家
LCR 187. 破冰游戏 - 力扣(LeetCode)
1143. 最长公共子序列 - 力扣(LeetCode)
21. 合并两个有序链表 - 力扣(LeetCode)
面试题 17.14. 最小K个数 - 力扣(LeetCode)
141. 环形链表 - 力扣(LeetCode)
165. 比较版本号 - 力扣(LeetCode)
点评
88. 合并两个有序数组 - 力扣(LeetCode)
优选
面试题 17.14. 最小K个数 - 力扣(LeetCode)
这一题注意
class Solution {
int k;
Random r=new Random();
public int[] smallestK(int[] arr, int _k) {
k = _k;
int n = arr.length;
int[] ans = new int[k];
if (k == 0) return ans;
partition(arr, 0, n - 1);
for (int i = 0; i < k; i++) ans[i] = arr[i];
return ans;
}
void swap(int[]nums,int left,int right)
{
int temp=nums[left];
nums[left]=nums[right];
nums[right]=temp;
}
void partition(int[]nums,int left,int right)
{
if(left>=right)return;
int pivotIndex=left+r.nextInt(right-left+1);
swap(nums, left, pivotIndex);
int i=left+1;
int j=right;
while(i<=j)
{
while(i<=j&&nums[i]<nums[left])
{
i++;
}
while(i<=j&&nums[j]>nums[left])
{
j--;
}
if(i>j)
{
break;
}
swap(nums, i, j);
i++;
j--;
}
swap(nums, left, j);
if(j>k)partition(nums, left, j-1);
if(j<k)partition(nums, j+1, right);
}
}