hot100(9)

ops/2025/2/9 10:52:33/

81.104. 二叉树的最大深度 - 力扣(LeetCode)

后序遍历,从下往上,需要用到下面返回的结果。

public int maxDepth(TreeNode root) {if(root == null){return 0;}int left = maxDepth(root.left);int right = maxDepth(root.right);return Math.max(left,right) + 1;}

82.102. 二叉树的层序遍历 - 力扣(LeetCode)

层序遍历,队列

List<List<Integer>> res = new ArrayList<>();public List<List<Integer>> levelOrder(TreeNode root) {if(root == null) return res;level(root);return res;}public void level(TreeNode root){Deque<TreeNode> queue = new ArrayDeque<>();queue.offer(root);while(!queue.isEmpty()){int len = queue.size();List<Integer> list = new ArrayList<>();for(int i = 0 ; i < len ; i++){TreeNode node = queue.poll();list.add(node.val);if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}}res.add(list);}}

83.101. 对称二叉树 - 力扣(LeetCode)

树形结构,且判断子树是否对称 与 判断原二叉树是否对称 是相同的问题,子问题与原文题具有相同的结构,考虑递归。

public boolean isSymmetric(TreeNode root) {return isSymmetricHelper(root.left,root.right);}public boolean isSymmetricHelper(TreeNode p,TreeNode q){if(p == null || q == null){return p == q;}return p.val == q.val && isSymmetricHelper(p.left,q.right) && isSymmetricHelper(p.right,q.left);}

84.98. 验证二叉搜索树 - 力扣(LeetCode)

中序遍历下,输出的二叉搜索树节点的数值是有序序列。

有了这个特性,验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。

TreeNode preNode;public boolean isValidBST(TreeNode root) {if(root == null) return true;boolean left = isValidBST(root.left);if(preNode != null && preNode.val >= root.val) return false;preNode = root;boolean right = isValidBST(root.right);return left && right;}

85.96. 不同的二叉搜索树 - 力扣(LeetCode)

题解:代码随想录_不同的二叉搜索树

1.dp数组及下标含义

dp[i] : 1到i为节点组成的二叉搜索树的个数为dp[i]个

或者i个不同元素的节点组成的二叉搜索树的数量为dp[i]个

2.确定递推公式

dp[i] += dp[以j为头节点左子树节点数量]*dp[以j为头节点右子树节点数量]

j相当于是头节点的元素,从1遍历到i为止

dp[i] += dp[j-1]*dp[i-j]

3.初始化

dp[0] = 1;

从定义上来讲,空节点也是一颗二叉树,也是一科二叉搜索树,可以说得通。

同时为了满足递推公式的乘法,避免结果都为0,dp[0]需要初始化为1

4.遍历顺序

i从大到小,遍历i里每一个数作为头节点的状态,用j来遍历

5.举例推导dp数组

public int numTrees(int n) {int[] dp = new int[n+1];dp[0] = 1;for(int i = 1 ; i <= n; i++){for(int j = 1 ; j <= i ; j++){dp[i] += (dp[j-1] * dp[i-j]);}}return dp[n];}

86.94. 二叉树的中序遍历 - 力扣(LeetCode)

List<Integer> res = new ArrayList<>();public List<Integer> inorderTraversal(TreeNode root) {inOrder(root);return res;}public void inOrder(TreeNode root){if(root == null){return;}inOrder(root.left);res.add(root.val);inOrder(root.right);}

87.85. 最大矩形 - 力扣(LeetCode)

题解:85.最大矩形题解 - 力扣

I暴力优化

class Solution {public int maximalRectangle(char[][] matrix) {int m = matrix.length;if (m == 0) {return 0;}int n = matrix[0].length;int[][] left = new int[m][n];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (matrix[i][j] == '1') {left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;}}}int ret = 0;for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {if (matrix[i][j] == '0') {continue;}int width = left[i][j];int area = width;for (int k = i - 1; k >= 0; k--) {width = Math.min(width, left[k][j]);area = Math.max(area, (i - k + 1) * width);}ret = Math.max(ret, area);}}return ret;}
}

单调栈

public int maximalRectangle(char[][] matrix) {int m = matrix.length;if(m == 0){return 0;}int n = matrix[0].length;int[][] left = new int[m][n];for(int i = 0 ; i < m ; i++){for(int j = 0 ; j < n ; j++){if(matrix[i][j] == '1'){left[i][j] = (j == 0 ? 0 : left[i][j-1]) + 1;}}}int res = 0;for(int j = 0 ; j < n ; j++){int[] up = new int[m];int[] down = new int[m];Deque<Integer> stack = new ArrayDeque<>();for(int i = 0 ; i < m;  i++){while(!stack.isEmpty() && left[stack.peek()][j] >= left[i][j]){stack.pop();}if(stack.isEmpty()){up[i] = -1;}else{up[i] = stack.peek();}stack.push(i);}stack.clear();for(int i = m - 1 ; i >= 0 ; i--){while(!stack.isEmpty() && left[stack.peek()][j] >= left[i][j]){stack.pop();}if(stack.isEmpty()){down[i] = m;}else{down[i] = stack.peek();}stack.push(i);}for(int i = 0 ; i < m ; i++){int height = down[i] - up[i] - 1;int area = height * left[i][j];res = Math.max(area,res);}}return res;}

88.84. 柱状图中最大的矩形 - 力扣(LeetCode)

单调栈

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

每个元素至多进出栈一次

时间复杂度O(n) 空间复杂度O(n)

单调栈+常数优化

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

时间复杂度O(n) 空间复杂度(n)

89.1. 两数之和 - 力扣(LeetCode)

哈希思想,空间换时间

public int[] twoSum(int[] nums, int target) {int[] res = new int[2];Map<Integer,Integer> map = new HashMap<>();for(int i = 0 ; i < nums.length ; i++){int match = target - nums[i];if(map.containsKey(match)){res[0] = map.get(match);res[1] = i;return res;}map.put(nums[i],i);}return res;}

90.78. 子集 - 力扣(LeetCode)

回溯问题

List<List<Integer>> res = new ArrayList<>();List<Integer> path = new ArrayList<>();int[] nums;public List<List<Integer>> subsets(int[] nums) {this.nums = nums;dfs(0);return res;}public void dfs(int index){res.add(new ArrayList<>(path));if(index == nums.length){return ;}for(int i = index ; i < nums.length ; i++){path.add(nums[i]);dfs(i+1);path.remove(path.size() - 1);}}


http://www.ppmy.cn/ops/156951.html

相关文章

优惠券平台(十三):基于注解和Spring AOP环绕通知实现去重表消息防止重复消费

业务背景 考虑这样两个场景&#xff1a; 消息 M 被发送到消息中间件并被消费者 A 接收&#xff0c;但在消费过程中系统重启&#xff0c;由于消息未被标记为成功消费&#xff0c;中间件会重复投递&#xff0c;直到消费成功&#xff0c;但可能导致消息被多次投递。 消费者 A 在…

解决错误:CondaHTTPError: HTTP 000 CONNECTION FAILED for url

解决错误&#xff1a;CondaHTTPError: HTTP 000 CONNECTION FAILED for url 查看channels:vim ~/.condarcshow_channel_urls: true channels:- http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/- http://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/…

Docker最佳实践:安装Nacos

文章目录 Docker最佳实践&#xff1a;安装Nacos一、引言二、安装 Nacos1、拉取 Nacos Docker 镜像2、启动 Nacos 容器 三、配置 Nacos&#xff08;可选&#xff09;四、使用示例1、服务注册2、服务发现 五、总结 Docker最佳实践&#xff1a;安装Nacos 一、引言 Nacos 是阿里巴…

腾讯云TI平台×DeepSeek:开启AI超强体验,解锁部署秘籍

引言 在人工智能飞速发展的今天&#xff0c;AI技术的应用场景已经渗透到我们生活的方方面面。从智能客服到自动驾驶&#xff0c;从精准医疗到金融科技&#xff0c;AI的应用正在不断推动各行业的变革与创新。作为AI领域的领军企业&#xff0c;腾讯云一直以来都在致力于为开发者…

.net framework 4.5 的项目,用Mono 部署在linux

步骤 1&#xff1a;安装 Mono 更新包列表&#xff1a; 首先&#xff0c;更新 Ubuntu 的包列表以确保获取最新的软件包信息。 sudo apt update 安装 Mono&#xff1a; 安装 Mono 完整版&#xff08;mono-complete&#xff09;&#xff0c;它包含了运行 .NET 应用程序所需的所有…

redis中的list类型

可以看作一个双向链表结构&#xff0c;支持正向和反向检索&#xff0c;有序&#xff0c;元素可以重复&#xff0c;插入和删除快&#xff0c;查询速度一般 list类型常见命令&#xff1a; LPUSH key element... : 向链表左侧插入一个或多个元素 LPOP key&#xff1a;移除并返回…

Vue 中的 nextTick 方法是什么?

Vue 中的 nextTick 方法 nextTick 是 Vue.js 提供的一个重要方法&#xff0c;用于在 DOM 更新后执行某个操作。它允许开发者在 Vue 组件的状态或数据发生变化后&#xff0c;延迟执行某段代码&#xff0c;确保 DOM 已经更新到最新状态。 目录 什么是 nextTick为什么使用 next…

【02】智能合约与虚拟机

Solidity底层 ABI接口详解 ABI是什么? ABI&#xff1a;Application Binary Interface&#xff08;应用程序二进制接口&#xff09; 蚂蚁链BaaS平台提供的Cloud IDE&#xff0c;会在合约编译后&#xff0c;一并生成对应的ABI文件&#xff08;JSON格式描述&#xff09; ABI…