LeetCode 525. Contiguous Array
525. Contiguous Array
Description Submission Solutions
- Total Accepted: 2476
- Total Submissions: 8220
- Difficulty: Medium
- Contributors: bishwa
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.
Example 1:
Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.
Example 2:
Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
Note: The length of the given binary array will not exceed 50,000.
Subscribe to see which companies asked this question.
【题目分析】
这个题目的意思是给定一个只包含0,1的整数数组,找到数组中最长的一个子序列,该序列中1的个数和0的个数相同。
【思路】
这个题目真的很有意思啊,我感觉自己想不到这么好的解法。
看大家的解题思路如下:
The idea is to change 0 in the original array to -1. Thus, if we find SUM[i, j] == 0 then we know there are even number of -1 and 1 between index i and j. Also put the sum to index mapping to a HashMap to make search faster.
【java代码】
public class Solution {
public int findMaxLength(int[] nums) {
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 0) nums[i] = -1;
}
int res = 0, sum = 0;
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
for(int i = 0; i < nums.length; i++) {
sum += nums[i];
if(map.containsKey(sum)) {
res = Math.max(res, i-map.get(sum));
}
else {
map.put(sum, i);
}
}
return res;
}
}
LeetCode 525. Contiguous Array的更多相关文章
- [LeetCode] 525. Contiguous Array 相连的数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- 【LeetCode】525. Contiguous Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- 【leetcode】525. Contiguous Array
题目如下: 解题思路:这个题目可以这么做,遍历数组,如果元素是0,则count --:否则count ++:这样的话,每遍历到一个下标i,count的值就是0>i区间内0和1的差值.如果我们能找 ...
- 525. Contiguous Array两位求和为1的对数
[抄题]: Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 ...
- 525. Contiguous Array
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- 525 Contiguous Array 连续数组
给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组.示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2:输入: [0,1, ...
- Contiguous Array with Equal Number of 0 & 1
2018-07-08 13:24:31 问题描述: 问题求解: 问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度.本题使用DP和滑动数组都比较棘手,这里给出的方案是p ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- [LeetCode] Contiguous Array 邻近数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
随机推荐
- Linux常见错误之Could not get lock /var/lib/dpkg/lock - open
在Ubuntu系统上安装vim是遇到的问题: root@ubuntu:/# vim The program 'vim' can be found in the following packages: ...
- 如何在VC6.0下用pthread.h这个头文件
如何在VC6.0下用pthread.h这个头文件 1.下载PTHREAD的WINDOWS开发包 pthreads-w32-2-4-0-release.exe(任何一个版本均可) http://so ...
- xampp mac 版安装
欢迎光临 XAMPP 的 Mac OS X 版 适用于 Mac OS X 的 XAMPP 是 Mac OS X 上最简单,最实用,也最完整的网络服务器解决方案.该发行版包括整合了最新的 MySQL.P ...
- Java设计模式—单例模式
Singleton是一种创建型模式,指某个类采用Singleton模式,则在这个类被创建后,只可能产生一个实例供外部访问,并且提供一个全局的访问点. 核心知识点如下: (1) 将采用单例设计模式的类的 ...
- python之路 django基础
Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Session等诸多功能. ...
- maven shade插件小记
maven shade plugin插件小用 项目中一直使用assembly插件来整合依赖包到一个胖jar,在做这个akka http项目的时候,在scala ide的run/debug中都执行正常, ...
- 伪类 :after 清除浮动的原理和方法
浮动元素容器的clearing问题1. 问题的由来有这样一种情形:在一个容器(container)中,有两个浮动的子元素.<div> <div style=" ...
- 关于文件中的0D、0A
文件一般分为文本文件和二进制文件. 在windows文本文件中,分行即‘\n“,表示为0x0D 0x0A.分为两种情况: 如果你想一个文本文件中写入一个'\n',文本文件中会增加0x0D 0x0A两个 ...
- 浅析SQL注入
body, td { font-family: calibri; font-size: 10pt; } 演示 记得以前瞎鼓捣的时候,学过一个传说中的SQL注入万能字符串,是这个样子的' or '1'= ...
- [转]HTML标签元素的分类
在讲解CSS布局之前,我们需要提前知道一些知识,在CSS中,html中的标签元素大体被分为三种不同的类型:块状元素.内联元素和内联块状元素. 常用的块状元素有: <div>.<p ...