一、力扣
! 1、删除二叉搜索树中的节点
450. 删除二叉搜索树中的节点

class Solution {
public TreeNode deleteNode(TreeNode root, int key) {
if (root == null) return null;
if (root.val < key) {
root.right = deleteNode(root.right, key);
} else if (root.val > key) {
root.left = deleteNode(root.left, key);
} else {
if (root.left == null) return root.right;
if (root.right == null) return root.left;
TreeNode ans = root.right;
while (ans.left != null) {
ans = ans.left;
}
root.val = ans.val;
root.right = deleteNode(root.right, ans.val);
}
return root;
}
}
2、每日温度
739. 每日温度

class Solution {
public int[] dailyTemperatures(int[] temperatures) {
int n = temperatures.length;
ArrayDeque<Integer> queue = new ArrayDeque<>();
int[] res = new int[n];
for (int i = n - 1; i >= 0; i--) {
while (!queue.isEmpty() && temperatures[queue.peek()] <= temperatures[i]) {
queue.pollFirst();
}
if (!queue.isEmpty())
res[i] = queue.peek() - i;
queue.offerFirst(i);
}
return res;
}
}
3、接雨水
42. 接雨水

class Solution {
public int trap(int[] height) {
int n = height.length;
int[] left = new int[n];
left[0] = height[0];
for (int i = 1; i < n; i++) {
left[i] = Math.max(left[i - 1], height[i]);
}
int[] right = new int[n];
right[n - 1] = height[n - 1];
for (int i = n - 2; i >= 0; i--) {
right[i] = Math.max(right[i + 1], height[i]);
}
int res = 0;
for (int i = 0; i < n; i++) {
res += Math.min(left[i], right[i]) - height[i];
}
return res;
}
}
4、柱状图中最大的矩形
84. 柱状图中最大的矩形

class Solution {
public int largestRectangleArea(int[] heights) {
int n = heights.length;
int[] left = new int[n];
ArrayDeque<Integer> stack = new ArrayDeque<>();
for (int i = 0; i < n; i++) {
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
stack.pop();
}
if (!stack.isEmpty()) {
left[i] = stack.peek();
} else {
left[i] = -1;
}
stack.push(i);
}
int[] right = new int[n];
stack = new ArrayDeque<>();
for (int i = n - 1; i >= 0; i--) {
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
stack.pop();
}
if (!stack.isEmpty()) {
right[i] = stack.peek();
} else {
right[i] = n;
}
stack.push(i);
}
int res = 0;
for (int i = 0; i < n; i++) {
res = Math.max(res, (right[i] - left[i] - 1) * heights[i]);
}
return res;
}
}