当前位置:网站首页>Leetcode brushes questions the next day
Leetcode brushes questions the next day
2022-07-27 18:55:00 【The sun is falling】
1. Print linked list from end to end
Enter the head node of a linked list , Return the value of each node from the end to the end ( Return with array ).
analysis :
Build a stack , First press each node in the linked list into the stack , Stack and print the value of each node .
class Solution {
public int[] reversePrint(ListNode head) {
LinkedList<Integer> stack = new LinkedList<Integer>();
while(head != null) {
stack.addLast(head.val);
head = head.next;
}
int[] res = new int[stack.size()];
for(int i = 0; i < res.length; i++)
res[i] = stack.removeLast();
return res;
}
}
2. Reconstruction of binary tree
Enter the results of preorder traversal and inorder traversal of a binary tree , Build the binary tree and return its root node .
It is assumed that the results of the input preorder traversal and the middle order traversal do not contain repeated numbers .
analysis :
The order of preorder traversal : The root node - The left subtree - Right subtree
The order in which the order is traversed : The left subtree - The root node - Right subtree
It can be seen that the first node of the preorder traversal must be the root node , According to this point and the characteristics of middle order traversal, we can judge which endpoints the left subtree has , What are the endpoints of the right subtree , Thus, the root node of the left subtree and the root node of the right subtree can be determined . According to this characteristic , Divide and conquer algorithm can be used .

Analysis of divide and conquer algorithm :
- Recursive parameter : Index traversed by the root node in the preorder root 、 The left boundary of the subtree traversal in the middle order left 、 The subtree traverses the right boundary of the middle order right ;
- Termination conditions : When left > right , Represents that you have crossed the leaf node , Return at this time null ;
- Recursive work : Establish root node node : The node value is preorder[root] ; Divide the left and right subtrees : Find the root node in the order traversal inorder Index in i ; To improve efficiency , This article uses hash table dic The mapping between the value of the order traversal and the index in the storage , The time complexity of the lookup operation is O(1)O(1) ;
- Construct left and right subtrees : Turn on left and right subtree recursion ;

- Return value : Backtracking return node , As the left node of the root node in the upper recursion / The right child node ;
class Solution {
int[] preorder;
HashMap<Integer, Integer> dic = new HashMap<>();
public TreeNode buildTree(int[] preorder, int[] inorder) {
this.preorder = preorder;
for(int i = 0; i < inorder.length; i++)
dic.put(inorder[i], i);
return recur(0, 0, inorder.length - 1);
}
TreeNode recur(int root, int left, int right) {
if(left > right) return null; // Recursive termination
TreeNode node = new TreeNode(preorder[root]); // Establish root node
int i = dic.get(preorder[root]); // Partition root node 、 The left subtree 、 Right subtree
node.left = recur(root + 1, left, i - 1); // Turn on left subtree recursion
node.right = recur(root + i - left + 1, i + 1, right); // Turn on right subtree recursion
return node; // Backtracking returns the root node
}
}
3. Queues are implemented with two stacks
Use two stacks to implement a queue . The declaration of the queue is as follows , Please implement its two functions appendTail and deleteHead , The functions of inserting integers at the end of the queue and deleting integers at the head of the queue are respectively completed .( If there are no elements in the queue ,deleteHead Operation return -1 ).
analysis :
Set up two stacks , One is input stack , One is output stack .
class CQueue {
Deque<Integer> inStack;
Deque<Integer> outStack;
public CQueue() {
inStack = new ArrayDeque<Integer>();
outStack = new ArrayDeque<Integer>();
}
public void appendTail(int value) {
inStack.push(value);
}
public int deleteHead() {
if (outStack.isEmpty()) {
if (inStack.isEmpty()) {
return -1;
}
in2out();
}
return outStack.pop();
}
private void in2out() {
while (!inStack.isEmpty()) {
outStack.push(inStack.pop());
}
}
}
4. Fibonacci sequence
Write a function , Input n , Fibonacci, please (Fibonacci) The number of the sequence n term ( namely F(N)). Fibonacci series is defined as follows :
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), among N > 1.
The Fibonacci series is composed of 0 and 1 Start , The Fibonacci number after that is the sum of the two numbers before .
The answer needs to be modelled 1e9+7(1000000007), If the initial result of calculation is :1000000008, Please return 1.
analysis :
Recursion will time out ( There will be a lot of double counting ). Adopt dynamic programming to solve .

Dynamic planning analysis :
- State definition : set up dp It's a one-dimensional array , among dp[i] The value of represents Fibonacci number one i A digital .
- Transfer equation : dp[i + 1] = dp[i] + dp[i - 1], That is, the definition of the corresponding sequence f(n + 1) = f(n) + f(n - 1) ;
- The initial state : dp[0] = 0, dp[1] = 1 , That is, initialize the first two numbers ;
- Return value : dp[n] , That is, the second of the Fibonacci sequence n A digital .
class Solution {
public int fib(int n) {
int a = 0, b = 1, sum;
for(int i = 0; i < n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}
5. The problem of frog jumping on the steps
A frog can jump up at a time 1 Stepped steps , You can jump on it 2 Stepped steps . Ask the frog to jump on one n How many jumps are there in the steps .
The answer needs to be modelled 1e9+7(1000000007), If the initial result of calculation is :1000000008, Please return 1.
analysis :
The law is consistent with Fibonacci sequence , namely f(n)=f(n-1)+f(n-2).
class Solution {
public int numWays(int n) {
int a = 1, b = 1, sum;
for(int i = 0; i < n; i++){
sum = (a + b) % 1000000007;
a = b;
b = sum;
}
return a;
}
}
边栏推荐
- MySQL 02 初体验
- 音乐律动七彩渐变灯芯片--DLT8S04A-杰力科创
- Wechat applet obtains mobile number
- Personal Center - order business process
- MySQL 04 advanced query (II)
- 迷你洗衣机触摸芯片-DLT8MA12TS-杰力科创
- ridis命令笔记
- js中的函数与DOM获取元素和事件属性的使用
- JDBC MySQL 01 JDBC operation MySQL (add, delete, modify and query)
- Household mute mosquito repellent lamp chip-dltap703sd-jericho
猜你喜欢
随机推荐
Whole body multifunctional massage instrument chip-dltap602sd
Uni app label jump
Order submission
EN 1155 building hardware swing door opener - CE certification
What does the number of network request interface layers (2/3 layers) mean
建木持续集成平台v2.5.2发布
多功能无线遥控艾灸仪芯片-DLTAP703SD
Collection of software design suggestions of "high cohesion and low coupling"
The song of the virtual idol was originally generated in this way!
MySQL 01 关系型数据库设计
Use ETL tools for data migration in salesforce project
商品推荐和分类商品推荐
Multifunctional wireless remote control moxibustion instrument chip dltap703sd
Interceptor拦截器
MySQL 02 初体验
音乐律动七彩渐变灯芯片--DLT8S04A-杰力科创
阿里p8总结的10条 SQL 优化方案(非常实用)
What should I do if MySQL master-slave replication data is inconsistent?
Build a simple knowledge question and answer system
Aircraft battle with enemy aircraft








