1
+ ---
2
+ title : 数组热题
3
+ date : 2025-01-08
4
+ tags :
5
+ - Array
6
+ - algorithms
7
+ - leetcode
8
+ categories : leetcode
9
+ ---
10
+
11
+ ![ ] ( https://cdn.pixabay.com/photo/2019/12/04/18/40/machined-parts-4673364_1280.jpg )
12
+
1
13
### [ 1. 两数之和] ( https://leetcode-cn.com/problems/two-sum/ )
2
14
3
15
> 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
@@ -77,6 +89,8 @@ public List<List<String>> groupAnagrams(String[] strs) {
77
89
78
90
79
91
92
+
93
+
80
94
### [ 128. 最长连续序列] ( https://leetcode.cn/problems/longest-consecutive-sequence/ )
81
95
82
96
> 给定一个未排序的整数数组 ` nums ` ,找出数字连续的最长序列(不要求序列元素在原数组中连续)的长度。
@@ -89,7 +103,7 @@ public List<List<String>> groupAnagrams(String[] strs) {
89
103
> 解释:最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。
90
104
> ```
91
105
92
- **思路**:哈希表
106
+ **思路**:哈希表,**每个数都判断一次这个数是不是连续序列的开头那个数**
93
107
94
108
```java
95
109
public int longestConsecutive(int[] nums) {
@@ -125,6 +139,51 @@ public int longestConsecutive(int[] nums) {
125
139
126
140
127
141
142
+ ### [ 560. 和为 K 的子数组] ( https://leetcode.cn/problems/subarray-sum-equals-k/ )
143
+
144
+ > 给你一个整数数组 ` nums ` 和一个整数 ` k ` ,请你统计并返回 * 该数组中和为 ` k ` 的子数组的个数* 。
145
+ >
146
+ > 子数组是数组中元素的连续非空序列。
147
+ >
148
+ > ```
149
+ > 输入:nums = [1,2,3], k = 3
150
+ > 输出:2
151
+ > ```
152
+
153
+ 思路:**前缀和 + 哈希表**
154
+
155
+ 1. **遍历数组**,逐步计算前缀和(表示从数组起始位置到当前位置的所有元素之和) `prefixSum`。
156
+ 2. 检查哈希表中是否存在 `prefixSum - k`:
157
+ - 若存在,累加其出现次数到结果 `count`。
158
+ 3. **更新哈希表**:将当前前缀和存入哈希表,若已存在则次数加 1。
159
+
160
+ ```java
161
+ class Solution {
162
+ public int subarraySum(int[] nums, int k) {
163
+ int count = 0;
164
+ int prefixSum = 0;
165
+ Map<Integer, Integer> sumMap = new HashMap<>();
166
+ sumMap.put(0, 1); // 初始化前缀和为0的计数为1
167
+
168
+ for (int num : nums) {
169
+ prefixSum += num;
170
+ // 检查是否存在前缀和为 prefixSum - k 的键
171
+ if (sumMap.containsKey(prefixSum - k)) {
172
+ count += sumMap.get(prefixSum - k);
173
+ }
174
+ // 更新当前前缀和的次数
175
+ sumMap.put(prefixSum, sumMap.getOrDefault(prefixSum, 0) + 1);
176
+ }
177
+ return count;
178
+ }
179
+ }
180
+ ```
181
+
182
+ - ** 时间复杂度** :O(n),每个元素遍历一次。
183
+ - ** 空间复杂度** :O(n),哈希表最多存储 n 个不同的前缀和。
184
+
185
+
186
+
128
187
### [ 14. 最长公共前缀] ( https://leetcode.cn/problems/longest-common-prefix/ )
129
188
130
189
> 编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串 ` "" ` 。
@@ -347,7 +406,7 @@ public int maxArea(int[] height){
347
406
**思路**:「连续」子数组,题目要求的是返回结果,用 [动态规划、分治]
348
407
349
408
```java
350
- public static int maxSubArray3 (int[] nums) {
409
+ public static int maxSubArray (int[] nums) {
351
410
//特判
352
411
if (nums == null || nums.length == 0) {
353
412
return 0;
@@ -381,6 +440,59 @@ public int maxSubArray(int[] nums) {
381
440
382
441
383
442
443
+ ### [ 56. 合并区间] ( https://leetcode.cn/problems/merge-intervals/ )
444
+
445
+ > 以数组 ` intervals ` 表示若干个区间的集合,其中单个区间为 ` intervals[i] = [starti, endi] ` 。请你合并所有重叠的区间,并返回 * 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间* 。
446
+ >
447
+ > ```
448
+ > 输入:intervals = [[1,3],[2,6],[8,10],[15,18]]
449
+ > 输出:[[1,6],[8,10],[15,18]]
450
+ > 解释:区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
451
+ > ```
452
+
453
+ 思路:
454
+
455
+ 1. **排序** 将区间按起始位置升序排列,使可能重叠的区间相邻,便于合并操作
456
+
457
+ 
458
+
459
+ 2. **合并逻辑**
460
+
461
+ **遍历判断**:依次遍历排序后的区间,比较当前区间与结果列表中的最后一个区间是否重叠:
462
+
463
+ - 重叠条件:当前区间起始 ≤ 结果列表中最后一个区间的结束。
464
+ - 合并操作:更新结果列表中最后一个区间的结束值为两者的最大值。
465
+ - **非重叠条件**:直接将当前区间加入结果列表
466
+
467
+ ```java
468
+ public int[][] merge(int[][] intervals) {
469
+ // 边界条件:空数组直接返回空
470
+ if (intervals.length == 0) return new int[0][];
471
+
472
+ // 按区间起始点升序排序
473
+ Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
474
+
475
+ // 结果列表
476
+ List<int[]> merged = new ArrayList<>();
477
+ merged.add(intervals[0]); // 初始化第一个区间
478
+
479
+ for (int i = 1; i < intervals.length; i++) {
480
+ int[] last = merged.get(merged.size() - 1);
481
+ int[] current = intervals[i];
482
+
483
+ if (current[0] <= last[1]) { // 重叠,合并区间
484
+ last[1] = Math.max(last[1], current[1]); // 更新结束值为较大值
485
+ } else { // 不重叠,直接添加
486
+ merged.add(current);
487
+ }
488
+ }
489
+
490
+ return merged.toArray(new int[merged.size()][]); // 转换为二维数组
491
+ }
492
+ ```
493
+
494
+
495
+
384
496
### [ 283. 移动零] ( https://leetcode-cn.com/problems/move-zeroes/ )
385
497
386
498
> 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
0 commit comments