Sword Finger Offer-Section 2.3-Data Structure

Sword Finger Offer-Section 2.3-Data Structure

This section mainly describes several data structures that are easy to examine in interviews. Mainly include: arrays, strings, linked lists, trees, stacks and queues.
This chapter involves a lot of subsequent topics, so it is suitable to read again after reading the complete book.

1. Repeated numbers in an array

The array occupies a contiguous memory space and stores data in order.
Therefore, any element can be read/written in O(1) time according to the subscript.
When using, pay special attention to the problem of array out-of-bounds.

T3 find repeated numbers in the array

Sword refers to P39
3 methods, optimal algorithm: time complexity, space complexity o(n)

method time complexity Space complexity
Sort the array nlogn
Hash table n n
Press to rearrange the array n 1
Do not modify the array: dichotomy sequentially counts the number of occurrences nlogn 1

T4 search in two-dimensional array

Two-dimensional array: increasing from top to bottom, increasing from left to right, find a number in it.

For solving complex problems, you can start with a specific example, and try to find universal laws by analyzing simple and specific examples.

Start the search from the upper right corner. If the number to be searched is less than the upper right corner, the current column will be removed. If the number with search is greater than the number in the upper right corner, the current line will be eliminated.
In this way, the search range is continuously narrowed.

2. String

Operations on String generally generate a new instance and return it in the return value.
Therefore, every modification to the String will produce a temporary object. Too much overhead will affect efficiency.
Therefore, STringBuilder is generally used to modify the string multiple times in succession.
For the problem of parameter passing in Java: https://blog.csdn.net/xiaohaigary/article/details/113396491

The space in the T5 replacement string is %20

The first thing to consider is whether to operate on the original string? If yes, is there enough space for string storage?
Requirement: Operate on the original string, and there is sufficient space.

Replacing from front to back will cause the characters in the latter part to be moved multiple times. O(nn) is
therefore considered, from back to front.

First traverse once, count the number of spaces, and multiply by 2 the expansion space needed in real time.
Then move from the back to the previous one in order. O(n)

To merge two arrays and strings, generally you can use a back-to-front approach.

3. Linked List

T6 prints the linked list from end to end:

Natural idea:
Reverse the linked list and traverse sequentially. NC78
but this approach will modify the structure of the linked list. Generally speaking, printing is access. Need to ask if the interviewer can make changes.

Primary idea:
Traverse the linked list, push them one by one, and then pop them one by one.
Robustness is strong.

Extension idea: The
stack is a recursive implementation. The essence of recursion is the stack.
Therefore, there can be a recursive idea:
every time a node is visited, the nodes after it are first recursively output, and then the node itself is output.
Disadvantage: When the linked list is very long, it may cause the function call stack to overflow.

4. Tree

You need to know the 6 ways of traversing in 3 of the tree well.
Binary search tree, heap, red-black tree, etc.

T7 Rebuild the binary tree according to the pre-order and middle-order

Using a recursive method, continuously divide the left and right subtrees and rebuild them separately.
Need to pay attention to the array range.

The next node of the T8 binary tree

Familiar with the middle-order traversal, distinguish each situation clearly, and discuss it separately.

5. Stacks and queues

Data structures with restricted operations.

T9 uses two stacks to implement queues

It is very similar to implementing a stack with two queues.

Similarities:
Enqueue operations are all put into one stack.
All push operations are put into one team.

The difference:
When leaving the team, all the elements in the A stack are poured into the B stack. Then you leave the team when you pop.
When popping from the stack, the A queue elements are sequentially moved into the B queue, and the last element in the A queue is discarded, that is, the stack is popped.

Guess you like

Origin blog.csdn.net/xiaohaigary/article/details/113407426