Sword refers to the summary of offer and leetcode brushing questions

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

insert image description here

Delete duplicate nodes in linked list

Need to pay attention to distinguish between equal and unequal

insert image description here

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.
insert image description here

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

insert image description here

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.

insert image description here

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.

insert image description here

path 2

I don't understand recursion

insert image description here

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.

insert image description here

Determine whether it is a balanced tree

This recursion is really disgusting.

insert image description here

Judge mirrored binary tree

insert image description here

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

insert image description here

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

insert image description here

stack structure

Use two stacks to realize the first-in-first-out of the queue

insert image description here

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.

insert image description here

Determine whether the pop and push of the stack are consistent

insert image description here

The maximum value of the sliding window

insert image description here

Use the stack to complete sentence reversal

insert image description here

binary search

insert image description here

Rotate the minimum value of the array

insert image description here

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.

insert image description here

dynamic programming

Frog jumping steps, Fibonacci sequence and rectangle covering problem

insert image description here

Maximum sum of consecutive subarrays

This question needs the maximum length, so you need to record the maximum length

insert image description here

buy stocks for maximum profit

insert image description here

buy gift max

Note that the for loop is the length of the dp array

insert image description here

translate numbers to strings

There are two cases, one is to translate 0 into a. Go straight to dynamic programming

insert image description here
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
insert image description here

Sorting Algorithm

reversed pairs in an array

Use merge sort, divide first, then sum, 4 parameters when dividing, and 5 parameters when summing

insert image description here

Use heap sort to find the smallest or largest numbers

insert image description here

median in data stream

insert image description here

bit operation

Addition without addition

insert image description here

insert image description here

other algorithms

The first unique character in the character stream.

insert image description here

Divide the array into odd numbers in the front and even numbers in the back

Version 1, not in the original order.
insert image description here
Version 2, requires the order to be in the original order.

leetcode

Add numbers in linked list
insert image description here

Longest string without repetition

insert image description here

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.

insert image description here

The maximum value of water

insert image description here

sum of three numbers

insert image description here

combination of phone numbers

insert image description here

Generation of parentheses

insert image description here

The next slightly larger permutation

insert image description here

insert image description here

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.

insert image description here

The sum of the array is equal to the target value (array elements can be used repeatedly)

//Don't forget to sort it first
insert image description here

catch rainwater

Construct a monotonically decreasing queue

full array

insert image description here

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









insert image description here

anagram grouping

insert image description here

jumping game

insert image description here

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.

insert image description here

sort by color

insert image description here

minimum covering substring

insert image description here

subset problem

insert image description here

The largest area of ​​a rectangle

insert image description here

おすすめ

転載: blog.csdn.net/m0_56184347/article/details/123679303