当前位置:网站首页>LeetCode85+105+114+124
LeetCode85+105+114+124
2022-06-29 22:07:00 【想进阿里的小菜鸡】
思路
每次遍历数组的一行,然后计算当前行及其以上行的高度为height数组,然后求的矩形最大面积就变为84题一样的类型了,直接套用84题的代码即可。
代码
class Solution {
public int maximalRectangle(char[][] matrix) {
int [] height = new int[matrix[0].length];
int index = 0;
int res = 0;
for(int i = 0;i <matrix.length;i++ ){
for(int j =0;j<matrix[i].length;j++){
if(matrix[i][j] == '0'){
height[j] = 0;
}else{
height[j] +=1;
}
}
res = Math.max(res,largestRectangleArea(height));
}
return res;
}
public int largestRectangleArea(int[] heights) {
Stack<Integer> stack = new Stack<>();
int res = 0;
for(int i = 0;i<=heights.length;i++){
int h =0;
if(i == heights.length){
h = 0;
}else{
h = heights[i];
}
while(!stack.isEmpty() && h<heights[stack.peek()]){
int height = heights[stack.pop()];
int start = (stack.isEmpty()) ? -1:stack.peek();
int area = (i-start-1)*height;
res = Math.max(res,area);
}
stack.push(i);
}
return res;
}
}思路
从前序遍历的第一个节点就是该二叉树的根节点,而前序遍历的顶点的左边,就是该顶点的左边的中序遍历,从顶点取左边中序遍历的长度的距离的点,就是顶点左边的前序遍历的集合。
右边也是同理。所以选用递归就比较容易实现上面的思路。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
Map<Integer,Integer> map = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length == 1){
return new TreeNode(preorder[0]);
}
for(int i =0;i<inorder.length;i++){
map.put(inorder[i],i);
}
TreeNode res = fun(preorder,inorder,0,preorder.length-1,0,inorder.length-1);
return res;
}
public TreeNode fun(int[] preorder, int[] inorder,int pl,int pr,int il,int ir){
if(pl > pr || il > ir){
return null;
}
int k = map.get(preorder[pl]) - il; // map.get(pre[pl])是中序遍历中根节点位置,k是子树前序和中序遍历的长度
TreeNode root = new TreeNode(preorder[pl]);
root.left = fun(preorder,inorder,pl+1,pl+k,il,il+k-1); //左子树前序遍历,左子树中序遍历
root.right = fun(preorder,inorder,pl+k+1,pr,il+k+1,ir); //右子树前序遍历,右子树中序遍历
return root;
}
}思路
首先就是从根节点开始,将所有节点依次入栈。然后将栈顶元素弹出记做cur,弹出后将栈顶元素的右孩子和左孩子依次入栈,接着判断下当前栈是否为空,如果不为空就将cur的右孩子指定为当前节点,其左孩子置为空。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public void flatten(TreeNode root) {
if(root == null){
return;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
if(cur.right != null) stack.push(cur.right);
if(cur.left != null) stack.push(cur.left);
if(!stack.isEmpty()){
cur.right = stack.peek();
}
cur.left = null;
}
}
}
思路
运用递归的方法来解决这个问题。递归函数定义为每次返回的结果是该节点最左或者最右子树的最大值+该节点的值。然后在递归函数中,用res来保存最后的结果。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int res = -10000;
public int maxPathSum(TreeNode root) {
if(root == null){
return 0;
}
fun(root);
return res;
}
public int fun(TreeNode root){
if(root == null){
return 0;
}
int left = Math.max(0,fun(root.left));
int right = Math.max(0,fun(root.right));
res = Math.max(res,(root.val+left+right));
return Math.max(left,right)+root.val;
}
}边栏推荐
- 华为云AOM 2.0版本发布
- Flame retardant test of aluminum sheet as/nzs 1530.1 non combustible materials
- 每日刷题记录 (八)
- Taro2.* applet configuration sharing wechat circle of friends
- Introduction to gaofen-3 satellite (GF-3)
- Is it appropriate to apply silicone paint to American Standard UL 790 class a?
- jfinal中如何使用过滤器监控Druid监听SQL执行?
- qt5.14.2连接ubuntu20.04的mysql数据库出错
- 低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
- DevCloud加持下的青软,让教育“智”上云端
猜你喜欢

为什么要同时重写hashcode和equals方法之简单理解

细说GaussDB(DWS)复杂多样的资源负载管理手段

【无工具搭建PHP8+oracle11g+Windows环境】内网/无网络/Win10/PHP连接oracle数据库实例

ASP.NET 跨页面提交(Button控件页面重定向)

一文2500字手把手教你使用jmeter进行分布式压力测试【保姆级教程】

科大讯飞 AI 学习机暑期新品发布会 AI + 教育深度结合再创产品新高度

cout 不明确问题

ASP using panel to realize simple registration page

ASP动态创建表格 Table

这次跟大家聊聊技术,也聊聊人生
随机推荐
Simple understanding of why to rewrite hashcode and equals methods at the same time
这次跟大家聊聊技术,也聊聊人生
Autodesk Revit 2023 software installation package download and installation tutorial
华为云AOM 2.0版本发布
If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview
动态规划学习(持续更新)
掌握这28张图,面试再也不怕被问TCP知识了
低代码、端到端,一小时构建IoT示例场景,声网发布灵隼物联网云平台
ASP using panel to realize simple registration page
static关键字续、继承、重写、多态
5分钟快速上手 pytest 测试框架
每日刷题记录 (八)
为什么要同时重写hashcode和equals方法之简单理解
How to make good use of data science?
Taro applet enables wxml code compression
State management uses session to restrict page access. Only login can verify sessionlogin Aspx can access session aspx
Underlying principles of file operations (file descriptors and buffers)
研发测试时间比,BUG数据分析
关于深度学习的概念理解(笔记)
Numpy array creation