当前位置:网站首页>LeetCode85+105+114+124
LeetCode85+105+114+124
2022-06-29 22:24:00 【Want to join Ali's Chicken】
Ideas
Iterate through the array one row at a time , Then calculate the height of the current row and its upper row height Array , Then the maximum area of the rectangle becomes 84 The same type of questions , Direct application 84 The code of the question can be .
Code
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;
}
}105. Construction of binary trees from traversal sequences of preorder and middle order
Ideas
The first node traversed from the preorder is the root node of the binary tree , And the left side of the vertex traversed by the preceding sequence , Is the middle order traversal to the left of the vertex , The point at which the distance of the length traversed by the left middle order is taken from the vertex , Is the set of preordered traversals on the left of the vertex .
The same is true on the right . Therefore, it is easier to implement the above idea by using recursion .
Code
/**
* 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]) Is the position of the root node in the middle order traversal ,k Is the length of the pre - and middle order traversal of the subtree
TreeNode root = new TreeNode(preorder[pl]);
root.left = fun(preorder,inorder,pl+1,pl+k,il,il+k-1); // Left subtree preorder traversal , Left subtree middle order traversal
root.right = fun(preorder,inorder,pl+k+1,pr,il+k+1,ir); // Right subtree preorder traversal , Order traversal in right subtree
return root;
}
}114. The binary tree is expanded into a list
Ideas
The first is to start from the root node , Stack all nodes in turn . Then pop up the top element of the stack and write cur, After pop-up, put the right child and the left child of the top element on the stack in turn , Then judge whether the current stack is empty , If it is not empty, the cur The right child of is designated as the current node , The left child is left blank .
Code
/**
* 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;
}
}
}
124. The maximum path in a binary tree and
Ideas
Use recursion to solve this problem . The recursive function is defined as that the result returned each time is the maximum value of the leftmost or rightmost subtree of the node + The value of this node . Then in the recursive function , use res To save the final results .
Code
/**
* 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;
}
}边栏推荐
- Final training simple address book c language
- 为什么要同时重写hashcode和equals方法之简单理解
- Spark集群安装
- The soft youth under the blessing of devcloud makes education "smart" in the cloud
- Low code, end-to-end, one hour to build IOT sample scenarios, and the sound network released lingfalcon Internet of things cloud platform
- Shangsilicon Valley real-time data warehouse project (Alibaba cloud real-time data warehouse)
- Dynamics 365online lookup lookup field multiple selection
- 2022年第一季度保险服务数字化跟踪分析
- 合宙AIR32F103CBT6开发板上手报告
- Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
猜你喜欢

5-1系统漏洞扫描

One click file sharing software jirafeau
![The inadvertently discovered [tidb cache table] can solve the read / write hotspot problem](/img/96/b1595b9d2b008b353765caa68fdd3c.png)
The inadvertently discovered [tidb cache table] can solve the read / write hotspot problem

2022 (第五届)GIS软件技术大会开幕,GIS、IT将加速融合

ASP动态创建表格 Table

中国数据库崛起,阿里云李飞飞:中国云数据库多种主流技术创新已领先国外

Simple understanding of why to rewrite hashcode and equals methods at the same time

每日刷题记录 (八)

生产环境AIX小机报错B6267342问题处理

The soft youth under the blessing of devcloud makes education "smart" in the cloud
随机推荐
Type of radar
中国数据库崛起,阿里云李飞飞:中国云数据库多种主流技术创新已领先国外
Numpy array creation
架构实战营毕业总结
为什么在局域网(ERP服务器)共享文件夹上拷贝文件时导致全局域英特网断网
从零实现深度学习框架——LSTM从理论到实战【理论】
文件操作的底层原理(文件描述符与缓冲区)
MooseFS 调优笔记
生产环境AIX小机报错B6267342问题处理
Build a short video platform, fade in and fade out, and support left sliding and right pulley to broadcast pictures
便携式4K音视频会议终端一体机带8倍数字变焦
leetcode:91. 解码方法【dfs + 记忆化】
Shangsilicon Valley real-time data warehouse project (Alibaba cloud real-time data warehouse)
Moosefs tuning notes
合宙AIR32F103CBT6开发板上手报告
Graduation summary of construction practice camp
软件快速交付真的需要以安全为代价吗?
A mysql IBD file is too large processing record
89. (cesium article) cesium aggregation diagram (custom picture)
How to use filters in jfinal to monitor Druid for SQL execution?