linked list
Replication of complex linked lists
The point to note is that
1, it is possible that the node will not have a random pointer, so it is necessary to exclude that situation. When copying the random pointer
2, when splitting the two linked lists, it needs to be specified. It is possible that the original linked list temp has reached the last one. need to be excluded
Delete duplicate nodes in linked list
Need to pay attention to distinguish between equal and unequal
Tree
Zigzag printing of binary tree
Use the double-ended queue to realize bfs, each time first judge how many elements there are in each layer, and then dequeue and add them to the linked list in turn.
According to the in-order traversal and pre-order traversal array, restore its tree
Note that the use of Arrays.copyOfRange() is left closed and right open
Comparing substructures of trees
At this point, you should first judge node2, and then judge node1. If node2 is empty, it means that the judgment is completed and contains substructures, but if node2 is not null, but node1 is null, it means that there is no substructure. You can directly returns false.
Whether the value on the path is sum
A simple BFS, note that the title requirement must be from the head node to the root node, so it needs to be judged and must be the root node.
path 2
I don't understand recursion
Convert tree to doubly linked list
The idea is to first output the results of the in-order traversal, and then create a new tree based on the results of the in-order traversal. Be careful not to reverse the left and right trees.
Determine whether it is a balanced tree
This recursion is really disgusting.
Judge mirrored binary tree
Find the common ancestor of two nodes in a binary tree
This question is still relatively complicated, because many data structures are needed to store the value of the node and its parent node.
When adding two child nodes to the collection, it means finding the child node, and then looking up one of the child nodes , unanimously find the root node, and then judge whether there is the same node in the parent node of another child node
nearest common node of binary search tree
This, using the above solution can also be solved, but another method is to use the characteristics of the binary search tree to search, the binary search tree is sorted, if the two target values are less than the root node, it
means The public node must be in the left subtree.
If one is larger than the root node and the other is smaller than the root node, then the current root node is the public parent node.
If both are larger than the root node, it means that the public node must be on the right
stack structure
Use two stacks to realize the first-in-first-out of the queue
Implement the min function with two stacks, when called, display the smallest element in the stack
The core code is that when the minimum value is always recorded in another stack, it is necessary to pop the stack and pop the stack.
Determine whether the pop and push of the stack are consistent
The maximum value of the sliding window
Use the stack to complete sentence reversal
binary search
Rotate the minimum value of the array
Given a string of numbers starting from 0, let you find out what number is the nth digit
The key is to understand the topic first, what is the nth digit, that is, you need to find the nth decimal number, and then determine a certain digit in the middle of this decimal number.
dynamic programming
Frog jumping steps, Fibonacci sequence and rectangle covering problem
Maximum sum of consecutive subarrays
This question needs the maximum length, so you need to record the maximum length
buy stocks for maximum profit
buy gift max
Note that the for loop is the length of the dp array
translate numbers to strings
There are two cases, one is to translate 0 into a. Go straight to dynamic programming
The other is to translate 1 into a, 0 cannot be translated, that is, 10 can only be translated into j, and cannot be split into 1 and 0
Sorting Algorithm
reversed pairs in an array
Use merge sort, divide first, then sum, 4 parameters when dividing, and 5 parameters when summing
Use heap sort to find the smallest or largest numbers
median in data stream
bit operation
Addition without addition
other algorithms
The first unique character in the character stream.
Divide the array into odd numbers in the front and even numbers in the back
Version 1, not in the original order.
Version 2, requires the order to be in the original order.
leetcode
Add numbers in linked list
Longest string without repetition
longest palindromic substring
Start from each character and look for both sides, first find the end of the repetition, then compare whether the leftmost end and the rightmost end are equal, then record the length, get the length and the starting position of the leftmost end, and then intercept it by substring. substring is left-closed and right-opened.
The maximum value of water
sum of three numbers
combination of phone numbers
Generation of parentheses
The next slightly larger permutation
Search the rotated array for the target value
The overall idea is to convert it into a search in an ordered array. Since it is locally ordered, one end of the split from the middle must be monotonous. If it is monotonous, first judge whether the target value is within this interval, and then narrow the range, and use the dichotomy method to find the target value from the monotone.
The sum of the array is equal to the target value (array elements can be used repeatedly)
//Don't forget to sort it first
catch rainwater
Construct a monotonically decreasing queue
full array
moving image
Case 1: Turn 90 degrees clockwise: First transpose and then mirror left and
right Left and right mirroring (first left and right and then up and down) 1 2 3 — 9 8 7 4 5 6 — 6 5 4 7 8 9 — 3 2 1 Situation 3: Turn 270 degrees clockwise: transpose first and then mirror up and down 1 2 3 — 3 6 9 4 5 6 — 2 5 8 7 8 9 — 1 4 7
anagram grouping
jumping game
merge interval
The difficulty lies in how to write a comparator to arrange the first numbers of all rows in the array in ascending order,
and when returning at the end, turn the result into a two-dimensional array and return it.