File tree Expand file tree Collapse file tree 3 files changed +90
-0
lines changed Expand file tree Collapse file tree 3 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -331,6 +331,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
331331- [ 0516. 最长回文子序列] ( ./problems/516.longest-palindromic-subsequence.md )
332332- [ 0513. 找树左下角的值] ( ./problems/513.find-bottom-left-tree-value.md ) 91
333333- [ 0518. 零钱兑换 II] ( ./problems/518.coin-change-2.md )
334+ - [ 0525. 连续数组] ( ./problems/525.contiguous-array.md ) 🆕
334335- [ 0547. 朋友圈] ( ./problems/547.friend-circles.md )
335336- [ 0560. 和为 K 的子数组] ( ./problems/560.subarray-sum-equals-k.md )
336337- [ 0609. 在系统中查找重复文件] ( ./problems/609.find-duplicate-file-in-system.md )
Original file line number Diff line number Diff line change 191191 - [ 0516. 最长回文子序列] ( ./problems/516.longest-palindromic-subsequence.md )
192192 - [ 0513. 找树左下角的值] ( ./problems/513.find-bottom-left-tree-value.md ) 91
193193 - [ 0518. 零钱兑换 II] ( ./problems/518.coin-change-2.md )
194+ - [ 0525. 连续数组] ( ./problems/525.contiguous-array.md ) 🆕
194195 - [ 0547. 朋友圈] ( ./problems/547.friend-circles.md )
195196 - [ 0560. 和为 K 的子数组] ( ./problems/560.subarray-sum-equals-k.md )
196197 - [ 0609. 在系统中查找重复文件] ( ./problems/609.find-duplicate-file-in-system.md )
Original file line number Diff line number Diff line change 1+ ## 题目地址(525. 连续数组)
2+
3+ https://leetcode-cn.com/problems/contiguous-array/
4+
5+ ## 题目描述
6+
7+ ```
8+ 给定一个二进制数组 nums , 找到含有相同数量的 0 和 1 的最长连续子数组,并返回该子数组的长度。
9+
10+
11+
12+ 示例 1:
13+
14+ 输入: nums = [0,1]
15+ 输出: 2
16+ 说明: [0, 1] 是具有相同数量0和1的最长连续子数组。
17+
18+ 示例 2:
19+
20+ 输入: nums = [0,1,0]
21+ 输出: 2
22+ 说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。
23+
24+
25+
26+ 提示:
27+
28+ 1 <= nums.length <= 105
29+ nums[i] 不是 0 就是 1
30+ ```
31+
32+ ## 前置知识
33+
34+ - 前缀和
35+
36+ ## 公司
37+
38+ - 暂无
39+
40+ ## 思路
41+
42+ ** 这道题的核心点在于将题目给的 0/1 数组转化为 -1/1 数组。**
43+
44+ 这样问题转化为求数组中的一段连续区间,其和等于 0。求出数组任意一段区间的和,我们可以使用前缀和数组来加快这个过程,问题转化为** 前缀和相同的两个最大区间长度** 。由于是求最大,那么使用哈希表记录** 第一次出现的位置** 就变得显而易见了。
45+
46+ > 我在 《91 天学算法》的哈希篇中总结了这个套路。
47+
48+ ## 关键点
49+
50+ - 将 0/1 数组转化为 -1/1 数组
51+
52+ ## 代码
53+
54+ - 语言支持:Python3
55+
56+ Python3 Code:
57+
58+ ``` python
59+ class Solution :
60+ def findMaxLength (self , A : List[int ]) -> int :
61+ A = [1 if a == 0 else - 1 for a in A]
62+ ans = acc = 0
63+ mapper = {0 : - 1 }
64+ for i in range (len (A)):
65+ acc += A[i]
66+ if acc not in mapper:
67+ mapper[acc] = i
68+ else :
69+ ans = max (ans, i - mapper[acc])
70+ return ans
71+ ```
72+
73+ ** 复杂度分析**
74+
75+ 令 n 为数组长度。
76+
77+ - 时间复杂度:$O(n)$
78+ - 空间复杂度:$O(n)$
79+
80+ > 此题解由 [ 力扣刷题插件] ( https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template ) 自动生成。
81+
82+ 力扣的小伙伴可以[ 关注我] ( https://leetcode-cn.com/u/fe-lucifer/ ) ,这样就会第一时间收到我的动态啦~
83+
84+ 以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
85+
86+ 关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你识别套路,高效刷题。
87+
88+ ![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg )
You can’t perform that action at this time.
0 commit comments