Hot100部分记录
215. 数组中的第K个最大元素
快排的思想 -> 快速选择
public class Solution {
private int quickSelect(List<Integer> nums, int k) {
// 随机选择基准数
Random rand = new Random();
int pivot = nums.get(rand.nextInt(nums.size()));
// 将大于、小于、等于 pivot 的元素划分至 big, small, equal 中
List<Integer> big = new ArrayList<>();
List<Integer> equal = new ArrayList<>();
List<Integer> small = new ArrayList<>();
for (int num : nums) {
if (num > pivot)
big.add(num);
else if (num < pivot)
small.add(num);
else
equal.add(num);
}
// 第 k 大元素在 big 中,递归划分
if (k <= big.size())
return quickSelect(big, k);
// 第 k 大元素在 small 中,递归划分
if (nums.size() - small.size() < k)
return quickSelect(small, k - nums.size() + small.size());
// 第 k 大元素在 equal 中,直接返回 pivot
return pivot;
}
public int findKthLargest(int[] nums, int k) {
List<Integer> numList = new ArrayList<>();
for (int num : nums) {
numList.add(num);
}
return quickSelect(numList, k);
}
}11. 盛最多水的容器
双指针法:指针初始化在数组两端, 每次移动较小的指针,直到两指针相遇。
class Solution {
public int maxArea(int[] height) {
int left = 0;
int right = height.length - 1;
int ans = 0;
while (left < right) {
int now = Math.min(height[left], height[right]) * (right - left);
ans = Math.max(ans, now);
if (height[left] > height[right]) {
right--;
}
else {
left++;
}
}
return ans;
}
}