当前位置:网站首页>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)
边栏推荐
- Process -- user address space and kernel address space
- We summarized the three recommendations for the use of Nacos and first published the Nacos 3.0 plan for the 4th anniversary of the open source of Nacos
- 科研环境对人的影响是很大的
- Golang startup error [resolved]
- 分布式开发漫谈
- The brutal rule of blackmail software continues, and attacks increase by 105%
- 正则过滤数据学习笔记(①)
- 为什么 BI 软件都搞不定关联分析
- 数据平台数据接入实践
- 覆盖接入2w+交通监测设备,EMQ为深圳市打造交通全要素数字化新引擎
猜你喜欢

Reinforcement learning (II): SARS, with code rewriting

移动通信——基于卷积码的差错控制系统仿真模型
![[golang] use select {}](/img/30/fa593ec682a40c47689c1fd88f9b83.png)
[golang] use select {}

Covering access to 2w+ traffic monitoring equipment, EMQ creates a new engine for the digitalization of all elements of traffic in Shenzhen

给LaTeX公式添加优美的注解;日更『数据科学』面试题集锦;大学生『计算机』自学指南;个人防火墙;前沿资料/论文 | ShowMeAI资讯日报

What is the ISO assessment? How to do the waiting insurance scheme

HCIA configuration instance (ENSP)

What are the common cyber threats faced by manufacturers and how do they protect themselves
![[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform](/img/e7/1d06eba0e50eeb91d2d5da7524f4af.png)
[10:00 public class]: application exploration of Kwai gpu/fpga/asic heterogeneous platform

Yocto project download and compilation
随机推荐
[WesternCTF2018]shrine
Code reading - ten C open source projects
Data platform data access practice
internship:用于类型判断的工具类编写
【Golang】- runtime.Goexit()
In depth analysis of C language memory alignment
[the road of Exile - Chapter 2]
Golang run times undefined error [resolved]
Network security litigation risk: four issues that chief information security officers are most concerned about
[the road of Exile - Chapter 7]
[the road of Exile - Chapter 6]
[golang] use select {}
It is found that the data of decimal type in the database can be obtained through resultset.getdouble, but this attribute cannot be obtained through GetObject.
【观察】三年跃居纯公有云SaaS第一,用友YonSuite的“飞轮效应”
Planning mathematics final exam simulation II
[7.27] code source - [deletion], [bracket sequence], [number replacement], [game], [painting]
Six simple techniques to improve the value of penetration testing and save tens of thousands of yuan
Super technology network security risk assessment service, comprehensively understand the security risks faced by the network system
Yocto project download and compilation
移动通信——基于卷积码的差错控制系统仿真模型