Skip to content

Commit fb6418a

Browse files
Merge pull request SharingSource#84 from SharingSource/ac_oier
✨feat: Add 413
2 parents 5440c67 + f49606e commit fb6418a

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

Index/双指针.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| [27. 移除元素](https://leetcode-cn.com/problems/remove-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-element/solution/shua-chuan-lc-shuang-bai-shuang-zhi-zhen-mzt8/) | 简单 | 🤩🤩🤩🤩 |
1111
| [45. 跳跃游戏 II](https://leetcode-cn.com/problems/jump-game-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/jump-game-ii/solution/xiang-jie-dp-tan-xin-shuang-zhi-zhen-jie-roh4/) | 中等 | 🤩🤩🤩🤩 |
1212
| [88. 合并两个有序数组](https://leetcode-cn.com/problems/merge-sorted-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/merge-sorted-array/solution/gong-shui-san-xie-yi-ti-san-jie-shuang-z-47gj/) | 简单 | 🤩🤩🤩 |
13+
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |
1314
| [395. 至少有 K 个重复字符的最长子串](https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-substring-with-at-least-k-repeating-characters/solution/xiang-jie-mei-ju-shuang-zhi-zhen-jie-fa-50ri1/) | 中等 | 🤩🤩🤩 |
1415
| [424. 替换后的最长重复字符](https://leetcode-cn.com/problems/longest-repeating-character-replacement/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-repeating-character-replacement/solution/ping-ping-wu-qi-shuang-zhi-zhen-da-bai-h-fgif/) | 中等 | 🤩🤩🤩🤩 |
1516
| [485. 最大连续 1 的个数](https://leetcode-cn.com/problems/max-consecutive-ones/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones/solution/you-shi-yi-tian-gao-pin-jian-dan-ti-ni-d-avj1/) | 简单 | 🤩🤩🤩🤩 |

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| [171. Excel表列序号](https://leetcode-cn.com/problems/excel-sheet-column-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/excel-sheet-column-number/solution/gong-shui-san-xie-tong-yong-jin-zhi-zhua-y5fm/) | 简单 | 🤩🤩🤩 |
2020
| [190. 颠倒二进制位](https://leetcode-cn.com/problems/reverse-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-bits/solution/yi-ti-san-jie-dui-cheng-wei-zhu-wei-fen-ub1hi/) | 简单 | 🤩🤩🤩 |
2121
| [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/ugly-number/solution/gong-shui-san-xie-jian-dan-de-fen-qing-k-dlvg/) | 简单 | 🤩🤩 |
22+
| [413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/) | 中等 | 🤩🤩🤩🤩 |
2223
| [451. 根据字符出现频率排序](https://leetcode-cn.com/problems/sort-characters-by-frequency/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sort-characters-by-frequency/solution/gong-shui-san-xie-shu-ju-jie-gou-yun-yon-gst9/) | 中等 | 🤩🤩🤩🤩 |
2324
| [457. 环形数组是否存在循环](https://leetcode-cn.com/problems/circular-array-loop/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/circular-array-loop/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-ag05/) | 中等 | 🤩🤩🤩🤩 |
2425
| [566. 重塑矩阵](https://leetcode-cn.com/problems/reshape-the-matrix/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reshape-the-matrix/solution/jian-dan-ti-zhong-quan-chu-ji-ke-yi-kan-79gv5/) | 简单 | 🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[413. 等差数列划分](https://leetcode-cn.com/problems/arithmetic-slices/solution/gong-shui-san-xie-shuang-zhi-zhen-qiu-ji-ef1q/)** ,难度为 **中等**
4+
5+
Tag : 「双指针」、「模拟」、「数学」
6+
7+
8+
9+
如果一个数列 至少有三个元素 ,并且任意两个相邻元素之差相同,则称该数列为等差数列。
10+
11+
例如,[1,3,5,7,9][7,7,7,7][3,-1,-5,-9] 都是等差数列。
12+
13+
给你一个整数数组 nums ,返回数组 nums 中所有为等差数组的 子数组 个数。
14+
15+
子数组 是数组中的一个连续序列。
16+
17+
18+
示例 1:
19+
```
20+
输入:nums = [1,2,3,4]
21+
22+
输出:3
23+
24+
解释:nums 中有三个子等差数组:[1, 2, 3]、[2, 3, 4] 和 [1,2,3,4] 自身。
25+
```
26+
示例 2:
27+
```
28+
输入:nums = [1]
29+
30+
输出:0
31+
```
32+
33+
提示:
34+
* 1 <= nums.length <= 5000
35+
* -1000 <= nums[i] <= 1000
36+
37+
---
38+
39+
### 双指针
40+
41+
具体的,我们可以枚举 $i$ 作为差值为 $d$ 的子数组的左端点,然后通过「双指针」的方式找到当前等差并最长的子数组的右端点 $j$,令区间 $[i, j]$ 长度为 $len$。
42+
43+
那么显然,符合条件的子数组的数量为:
44+
45+
$$
46+
cnt = \sum_{k = 3}^{len}countWithArrayLength(k)
47+
$$
48+
49+
函数 `int countWithArrayLength(int k)` 求的是长度为 $k$ 的子数组的数量。
50+
51+
不难发现,随着入参 $k$ 的逐步减小,函数返回值逐步增大。
52+
53+
因此上述结果 $cnt$ 其实是一个 **首项为 $1$,末项为 $len - 3 + 1$,公差为 $1$ 的等差数列的求和结果**。直接套用「等差数列求和」公式求解即可。
54+
55+
代码:
56+
```Java
57+
class Solution {
58+
public int numberOfArithmeticSlices(int[] nums) {
59+
int n = nums.length;
60+
int ans = 0;
61+
for (int i = 0; i < n - 2; ) {
62+
int j = i, d = nums[i + 1] - nums[i];
63+
while (j + 1 < n && nums[j + 1] - nums[j] == d) j++;
64+
int len = j - i + 1;
65+
// a1:长度为 len 的子数组数量;an:长度为 3 的子数组数量
66+
int a1 = 1, an = len - 3 + 1;
67+
// 符合条件(长度大于等于3)的子数组的数量为「差值数列求和」结果
68+
int cnt = (a1 + an) * an / 2;
69+
ans += cnt;
70+
i = j;
71+
}
72+
return ans;
73+
}
74+
}
75+
```
76+
* 时间复杂度:$O(n)$
77+
* 空间复杂度:$O(1)$
78+
79+
---
80+
81+
### 最后
82+
83+
这是我们「刷穿 LeetCode」系列文章的第 `No.413` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先将所有不带锁的题目刷完。
84+
85+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
86+
87+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
88+
89+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
90+

0 commit comments

Comments
 (0)