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 2: Improvement of bubble sorting: take K times
Solution 3: Heap sort / quick sort / merge sort
NC118: Reversed pairs in the array
NC73: Numbers that occur more than half of the time in the array
Solution 3: Boyer-Moore voting algorithm
Idea: Traverse the comparison from back to front
1. NC61: the sum of two numbers:
2.①NC22: Combine two ordered arrays:
Solution: the idea of merging and sorting
2. ②NC37: Consolidation interval
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 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
6. ②NC30: The first positive number missing
7.NC128: The problem of water in the container
Solution 2: Preprocess the array
Solution 3: Double pointer method
8. The maximum product of three numbers
9.NC143: Matrix Multiplication
10. NC95: The longest continuous subsequence in the array
Solution 2: Use set collection
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 2: Dynamic programming
14.NC138: The longest incremental path of the matrix
15.NC131: Find the median of the data stream at any time
16.①NC7: The best time to buy and sell stocks (one transaction):
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
17.NC93: Design LRU cache structure
Solution 1: Use queue and map to achieve