Array
[toc]
Array
The memory addresses of array elements are contiguous.
Contiguous Memory Addresses of 2D Arrays
Memory management differs across programming languages.
C++ 2D Array:
- Contiguous memory allocation.
- Efficient for accessing elements in row-major order.
Java 2D Array:
像Java是没有指针的,同时也不对程序员暴露其元素的地址,寻址操作完全交给虚拟机。
所以看不到每个元素的地址情况
Non-contiguous memory allocation.
Each row is a distinct array object, leading to potential memory fragmentation.
Offers flexibility in handling rows of varying lengths (jagged arrays).
704. Binary Search
Given an array of integers nums
which is sorted in ascending order, and an integer target
, write a function to search target
in nums
. If target
exists, then return its index. Otherwise, return -1
.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
1 | Input: nums = [-1,0,3,5,9,12], target = 9 |
✨
Understanding Interval Rules:
- Inclusive-exclusive rule: The search interval is [left, right) where left is inclusive and right is exclusive.
- Inclusive-inclusive rule: The search interval is [left, right] where both left and right are inclusive.
- Understand how to correctly calculate mid based on these interval rules.
1 | class Solution { |
35. Search Insert Position
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
1 | Input: nums = [1,3,5,6], target = 5 |
Example 2:
1 | Input: nums = [1,3,5,6], target = 2 |
Example 3:
1 | Input: nums = [1,3,5,6], target = 7 |
1 | class Solution { |
34. Find First and Last Position of Element in Sorted Array
Medium
Given an array of integers nums
sorted in non-decreasing order, find the starting and ending position of a given target
value.
If target
is not found in the array, return [-1, -1]
.
You must write an algorithm with O(log n)
runtime complexity.
Example 1:
1 | Input: nums = [5,7,7,8,8,10], target = 8 |
Example 2:
1 | Input: nums = [5,7,7,8,8,10], target = 6 |
Example 3:
1 | Input: nums = [], target = 0 |
1 | class Solution { |
27. Remove Element
Input: nums = [3,2,2,3], val = 3
Output: 2, nums = [2,2,,]
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
It does not matter what you leave beyond the returned k (hence they are underscores).
✨
two pointers
1 | class Solution { |
977. Squares of a Sorted Array
Given an integer array nums
sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.
Example 1:
1 | Input: nums = [-4,-1,0,3,10] |
1 | // O(nlogn) |
✨
two pointers:
The maximum value of the squares of the array elements will be at either end of the array, either the leftmost or the rightmost element, and it cannot be in the middle.
209. Minimum Size Subarray Sum
Medium
Given an array of positive integers nums
and a positive integer target
, return the minimal length of a
subarray
whose sum is greater than or equal to target
. If there is no such subarray, return 0
instead.
✨ subarray is continuous. can’t change the index order.
Example 1:
1 | Input: target = 7, nums = [2,3,1,2,4,3] |
1 | // sliding window |
Let’s walk through the process of finding the minimum size subarray sum that exceeds the target sum
7
for the array[2, 3, 1, 2, 4, 3]
.Sliding Window Technique:
- Involves using a window (often defined by two pointers) to solve array or sequence-related problems.
- Utilizes the window to perform operations or find solutions.
- Can be considered an application of the two pointers technique, as it often involves moving two pointers to define and slide the window.
Two Pointers Technique:
- Involves using two pointers to traverse an array or sequence, typically towards each other.
- Used to find pairs or subsequences that satisfy certain conditions.
- May not always involve defining a window, unlike the sliding window technique.
59. Spiral Matrix II
Medium
Given a positive integer n
, generate an n x n
matrix
filled with elements from 1
to n2
in spiral order.
Example 1:
1 | Input: n = 3 |
1 | class Solution { |
✨模拟转圈
Pay attention to boundary issues.
这里一圈下来,我们要画每四条边,这四条边怎么画,每画一条边都要坚持一致的左闭右开,或者左开右闭的原则,这样这一圈才能按照统一的规则画下来。
So, following the left-closed and right-open principle, let me draw a circle.
这里每一种颜色,代表一条边,我们遍历的长度,可以看出每一个拐角处的处理规则,拐角处让给新的一条边来继续画。