当前位置:网站首页>leetcode/0和1个数相同的连续子数组
leetcode/0和1个数相同的连续子数组
2022-07-29 01:08:00 【xcrj】
代码
package com.xcrj;
import java.util.HashMap;
import java.util.Map;
/** * 0和1个数相同的子数组 * 给定一个二进制数组 nums , 找到含有相同数量的0和1的最长连续子数组,并返回该子数组的长度。 */
public class Solution11 {
/** * 前缀和+散列表 * nums[]中元素0认为是-1,元素1认为是1 * <preSum,对应下标>,preSum(0~下标) * preSum[j]+k=preSum[i],若preSum[i]等于preSum[j]则k=0,所以“下标j到下标i”元素之和为0。元素个数为下标i-下标j */
public int findMaxLength(int[] nums) {
// <preSum,对应下标>,preSum(0~下标)
Map<Integer, Integer> map = new HashMap<>(3);
// 元素0认为是-1,元素1认为是1。前缀和
int preSum = 0;
// <没有前缀,对应下标-1>
map.put(preSum, -1);
int maxLen = 0;
for (int i = 0; i < nums.length; i++) {
if (0 == nums[i]) preSum--;
else preSum++;
// preSum[j]+k=preSum[i],若preSum[i]等于preSum[j]则k=0,所以“下标j到下标i”元素之和为0。元素个数为下标i-下标j
if (map.containsKey(preSum)) {
maxLen = Math.max(maxLen, i - map.get(preSum));
} else {
// <preSum,对应下标>,preSum(0~下标)
map.put(preSum, i);
}
}
return maxLen;
}
public static void main(String[] args) {
Solution11 solution11 = new Solution11();
System.out.println(solution11.findMaxLength(new int[]{
0, 0, 1, 0, 0, 0, 1, 1}));
}
}
参考
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/A1NYOS/solution/0-he-1-ge-shu-xiang-tong-de-zi-shu-zu-by-xbyt/
来源:力扣(LeetCode)
边栏推荐
- [search] - iteration deepening / bidirectional dfs/ida*
- 【golang】使用select {}
- Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
- 5g commercial third year: driverless "going up the mountain" and "going to the sea"
- [the road of Exile - Chapter 4]
- 为什么 BI 软件都搞不定关联分析
- Sword finger offer special assault edition day 13
- 抓包工具Charles使用
- OpenGL development with QT (II) drawing cube
- 数学建模——公交调度优化
猜你喜欢

Reinforcement learning (I): Q-learning, with source code interpretation

【Golang】- runtime.Goexit()

Stonedb invites you to participate in the open source community monthly meeting!

Where will Jinan win in hosting the first computing power conference?

Planning mathematics final exam simulation II

【GoLang】同步锁 Mutex

Alphafold revealed the universe of protein structure - from nearly 1million structures to more than 200million structures
![About df['a column name'] [serial number]](/img/e2/179fb4eda695726e87bb483f65e04e.png)
About df['a column name'] [serial number]

Why does stonedb dare to call it the only open source MySQL native HTAP database in the industry?

Data platform data access practice
随机推荐
【流放之路-第七章】
数学建模——红酒品质分类
ELS stop at all
[understanding of opportunity-54]: plain book-1-the origin of things [original chapter 1]: the road is simple.
internship:用于类型判断的工具类编写
ELS new box falls
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
九天后我们一起,聚焦音视频、探秘技术新发展
移动通信——基于卷积码的差错控制系统仿真模型
How to choose professional, safe and high-performance remote control software
Come on, handwritten RPC S2 serialization exploration
[golang] synchronization lock mutex
Use POI to export excel file, image URL to export file, image and excel file to export compressed package
Data security is a competitive advantage. How can companies give priority to information security and compliance
覆盖接入2w+交通监测设备,EMQ为深圳市打造交通全要素数字化新引擎
Lxml web page capture the most complete strategy
The scientific research environment has a great impact on people
[WesternCTF2018]shrine
Golang run times undefined error [resolved]
Planning mathematics final exam simulation II