给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组。
示例 1:
输入: [0,1]
输出: 2
说明: [0, 1] 是具有相同数量0和1的最长连续子数组。

示例 2:
输入: [0,1,0]
输出: 2
说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。
注意: 给定的二进制数组的长度不会超过50000。
详见:https://leetcode.com/problems/contiguous-array/description/

Java实现:

class Solution {
public int findMaxLength(int[] nums) {
int res=0;
int n=nums.length;
int sum=0;
Map<Integer,Integer> m=new HashMap<Integer,Integer>();
m.put(0,-1);
for(int i=0;i<n;++i){
sum+=(nums[i]==1)?1:-1;
if(m.containsKey(sum)){
res=Math.max(res,(i-m.get(sum)));
}else{
m.put(sum,i);
}
}
return res;
}
}

C++实现:

class Solution {
public:
int findMaxLength(vector<int>& nums)
{
int res = 0, n = nums.size(), sum = 0;
unordered_map<int, int> m{{0, -1}};
for (int i = 0; i < n; ++i)
{
sum += (nums[i] == 1) ? 1 : -1;
if (m.count(sum))
{
res = max(res, i - m[sum]);
}
else
{
m[sum] = i;
}
}
return res;
}
};

参考:http://www.cnblogs.com/grandyang/p/6529857.html

525 Contiguous Array 连续数组的更多相关文章

  1. LeetCode 525. Contiguous Array

    525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...

  2. 【LeetCode】525. Contiguous Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...

  3. [LeetCode] 525. Contiguous Array 相连的数组

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  4. [LeetCode] Contiguous Array 邻近数组

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  5. 994.Contiguous Array 邻近数组

    描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...

  6. 525. Contiguous Array两位求和为1的对数

    [抄题]: Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 ...

  7. 525. Contiguous Array

    Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...

  8. 【leetcode】525. Contiguous Array

    题目如下: 解题思路:这个题目可以这么做,遍历数组,如果元素是0,则count --:否则count ++:这样的话,每遍历到一个下标i,count的值就是0>i区间内0和1的差值.如果我们能找 ...

  9. Contiguous Array with Equal Number of 0 & 1

    2018-07-08 13:24:31 问题描述: 问题求解: 问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度.本题使用DP和滑动数组都比较棘手,这里给出的方案是p ...

随机推荐

  1. HDU 6040 Hints of sd0061 nth_element函数

    Hints of sd0061 Problem Description sd0061, the legend of Beihang University ACM-ICPC Team, retired ...

  2. 倒排索引 获取指定单词的文档集合 使用hash去重单词term 提高数据压缩率的方法

    倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项都包括一个属性值和具有该属性值的各记录的地址.由于不是由记录来确定属性值,而是由属性值来确定记录的位置,因而称为倒排索引(inve ...

  3. UDP 端到端

    创建发送端 1.建立DatagramSocket对象,该端点建立,系统会随机分配一个端口,如果不想随机分配,可手动指定. 2.将数据进行packet封装,必须指定目的地址和端口. 3.通过socket ...

  4. java 获取项目根目录

    代码入下: request.getSession().getServletContext().getRealPath(); 这里的getRealPath()括号里面可以输入你想得到的具体目录. 需要注 ...

  5. c语言基本函数

    1. 用宏定义写出swap(x,y) #define swap(x, y) x = x + y; y = x - y; x = x - y; 2.数组a[N],存放了1至N-1个数,其中某个数重复一次 ...

  6. android按压背景

    android:background="?android:actionBarItemBackground"

  7. Java类成员访问控制权限

    类成员访问控制权限 在JAVA中有四种访问控制权限,分别为:private, default, protected, public 1.Private 如果一个成员方法或变量名前使用了private, ...

  8. Objective-C Runtime(二)消息传递机制

    在对象上调用方法是包括Objective-C的众多语言都具备的功能.但在Objective-C中,这个术语叫『传递消息』(pass a message).『消息』有「名称」(name)或「选择子」(s ...

  9. 【HDU 2089】 不要62

    [题目链接] 点击打开链接 [算法] 数位DP 和上一题 : HDU3555很像 [代码] #include<bits/stdc++.h> using namespace std; #de ...

  10. 关于spring boot打出的jar包在Linux中运行

    众所周知, spring boot打出的jar包可以通过 "java -jar xxx.jar"的方式来运行 但是在Linux中, 通过这个命令运行的话会占用该窗口, 当我们 Ct ...