Array related issues summary

Document: Knowledge Points-5-Array.note (Open Method: Youdao Cloud Notes, copy the link to the browser)
link: http://note.youdao.com/noteshare?id=a72b8b64a18ef13abb18f782a84c403f&sub=DA2F5EB3E8654349A0EA1474D8F07D41

table of Contents


NC119: the smallest number of K

Solution 1: Priority queue

Solution 2: Improvement of bubble sorting: take K times

Solution 3: Heap sort / quick sort / merge sort

NC118: Reversed pairs in the array

Solution 1: merge sort

Solution 2: Violent solution

NC73: Numbers that occur more than half of the time in the array

Solution 1: Sort

Solution 2: Hash method

Solution 3: Boyer-Moore voting algorithm

NC107: Find the peak

Idea: Traverse the comparison from back to front

1. NC61: the sum of two numbers:

Solution 1: Violent solution:

Solution 2: Hash table

2.①NC22: Combine two ordered arrays:

Solution: the idea of ​​merging and sorting

2. ②NC37: Consolidation interval

3.①NC38: spiral matrix

Idea: printEdge prints the outer layer of the matrix, printing layer by layer

3. ②NC18: Rotate the matrix clockwise:

Solution 1: Cycle layer by layer, coordinate transformation

Solution 2: Do two flips, first flip along the diagonal line from top right to bottom left, and then flip up and down along the horizontal center line

Solution 3: Find the law: mat[i][j] is rotated to the position of mat[j][ni-1]

4. NC54: triples whose sum is 0 in the array

5. NC48: Find the target value in the rotated ordered array

Solution 1: Binary search

Solution 2: Sort + Hash:

Solution 3: Violence

6. ①NC101: Missing numbers

Solution 1: Array/Hash

Solution 2: Sort

Solution 3: Bit operation

Solution 4: Divide

6. ②NC30: The first positive number missing

Idea 1: Count and sort

Idea 2: The maximum number returned will not exceed the length of the array plus one, and traverse sequentially

Idea 3: Violent solution

7.NC128: The problem of water in the container

Solution 1: Violence

Solution 2: Preprocess the array

Solution 3: Double pointer method

8. The maximum product of three numbers

Solution 1: Sort

Solution 2: Linear scan

9.NC143: Matrix Multiplication

10. NC95: The longest continuous subsequence in the array

Solution 1: Sort + Statistics

Solution 2: Use set collection

11.①NC110: Rotate array

Solution: three flips

11.②NC77: Adjust the array order so that odd numbers are in front of even numbers

Solution 1: Create two arrays, one for odd numbers, one for even numbers, and finally merge

Solution 2: Bubble sort can also ensure the relative position remains unchanged

Solution 3: End-to-end double pointer / fast and slow double pointer

12.NC19: Maximum cumulative sum of sub-arrays

Solution 1: Greedy

Solution 2: Dynamic programming

13.NC125: The problem of the longest sub-array series whose cumulative sum is a given value in an unsorted array

Solution: hash

Extended question 1:

Extended question 2:

14.NC138: The longest incremental path of the matrix

15.NC131: Find the median of the data stream at any time

Solution: Construct two priority queues: the large root on the left is stacked with smaller numbers, and the small root on the right is stacked with larger numbers.

16.①NC7: The best time to buy and sell stocks (one transaction):

Idea: Save each minimum point, update and maintain the difference between the larger value and the minimum value after the value

16.②NC134: The best time to buy and sell stocks (unlimited transactions):

Idea: Determine whether the neighbors are increasing

16.③NC135: The best time to buy and sell stocks (2 transactions):

Solution 1: Buy represents the return when buying, and sell represents the return when selling

Solution 2: Two cycles of positive and negative

Solution 3: Violent solution

17.NC93: Design LRU cache structure

Solution 1: Use queue and map to achieve

 

Guess you like

Origin blog.csdn.net/lixiaohulife/article/details/112985759