当前位置:网站首页>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;
}
}边栏推荐
- Automatic reply of wechat bulletin number intelligent reply with Turing robot
- 软件快速交付真的需要以安全为代价吗?
- [force deduction 10 days SQL introduction] day7+8 calculation function
- Three development trends of enterprise application viewed from the third technological revolution
- Don't worry about form deformation anymore
- Detailed explanation of MySQL and mvcc and the difference between RC and RR for snapshot reading
- ASP. Net cross page submission (button control page redirection)
- This time, I will talk about technology and life
- Motianlun "high availability architecture" dry goods document sharing (including 124 Oracle, MySQL and PG materials)
- 26 years old, 0 basic career change software test, from 3K to 16K monthly salary, a super complete learning guide compiled by me
猜你喜欢

IFLYTEK AI learning machine summer new product launch AI + education depth combination to create a new height of products

Shangsilicon Valley real-time data warehouse project (Alibaba cloud real-time data warehouse)

免费将pdf转换成word的软件分享,这几个软件一定要知道!

Three development trends of enterprise application viewed from the third technological revolution
![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

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

合宙AIR32F103CBT6开发板上手报告

What are the software testing methods and technical knowledge points?

If you master these 28 charts, you will no longer be afraid to be asked about TCP knowledge during the interview

Structure the fifth operation of the actual camp module
随机推荐
In the shop project, implement a menu (add, delete, modify and query)
26 years old, 0 basic career change software test, from 3K to 16K monthly salary, a super complete learning guide compiled by me
掌握这28张图,面试再也不怕被问TCP知识了
Unicom warehousing | all Unicom companies that need to sell their products need to enter the general warehouse first
math_基本初等函数图型(幂函数/指数/对数/三角/反三角)
Reading notes on how to connect the network - LAN on the server side (4)
Underlying principles of file operations (file descriptors and buffers)
一键式文件共享软件Jirafeau
One click file sharing software jirafeau
Moosefs tuning notes
从零实现深度学习框架——RNN从理论到实战【实战】
合宙AIR32F103CBT6开发板上手报告
Structure the fifth operation of the actual camp module
Dynamic planning learning (continuous update)
[multithreading] how to implement timer by yourself
The correct method for Navicat to connect to mysql8.0 (valid for personal testing)
[force deduction 10 days SQL introduction] day7+8 calculation function
交友平台小程序制作开发代码分享
Automatic reply of wechat bulletin number intelligent reply with Turing robot
If I am in Zhuhai, where can I open an account? Is it safe to open an account online?