525 Contiguous Array 连续数组
给定一个二进制数组, 找到含有相同数量的 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 连续数组的更多相关文章
- LeetCode 525. Contiguous Array
525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...
- 【LeetCode】525. Contiguous Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- [LeetCode] 525. Contiguous Array 相连的数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- [LeetCode] Contiguous Array 邻近数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- 994.Contiguous Array 邻近数组
描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...
- 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. ...
- 【leetcode】525. Contiguous Array
题目如下: 解题思路:这个题目可以这么做,遍历数组,如果元素是0,则count --:否则count ++:这样的话,每遍历到一个下标i,count的值就是0>i区间内0和1的差值.如果我们能找 ...
- Contiguous Array with Equal Number of 0 & 1
2018-07-08 13:24:31 问题描述: 问题求解: 问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度.本题使用DP和滑动数组都比较棘手,这里给出的方案是p ...
随机推荐
- 【bzoj3175】[Tjoi2013]攻击装置
每两个能互相攻击且能放置的点连一条双向边,然后跑二分图最大点独立集即可 #include<algorithm> #include<iostream> #include<c ...
- 使用Microsoft Office 2007将文档转换为PDF
点击帮助 输入关键词PDF后搜索 点击进入Save or convert to PDF or XPS 点击进入2007 Microsoft Office Add-in: Microsoft Save ...
- mongodb的安装、配置、常见问题
一.MongoDB下载 mongodb可以在官网找到下载链接,找到合适的版本进行下载.下载地址->https://www.mongodb.com/download-center?jmp=nav# ...
- hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
#1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a cir ...
- redis02---对于key的操作命令
Redis对于key的操作命令 del key1 key2 ... Keyn 作用: 删除1个或多个键 返回值: 不存在的key忽略掉,返回真正删除的key的数量 rename key newkey ...
- 666 专题五 AC自动机
Problem A.Keywords Search d.n个关键字,1段描述,求描述中出现了多少关键字 s. c. /* ac自动机模板 n个关键字,1段描述,求描述中出现了多少关键字 */ #inc ...
- docker hub下载慢解决方法 使用daocloud的mirror
见:http://blog.csdn.net/dingsai88/article/details/52638758
- SPOJ:Another Version of Inversion(二维数组的逆序对)
DCE Coders admins are way much geekier than they actually seem! Kartik has been following that tradi ...
- SPOJ:Bits. Exponents and Gcd(组合数+GCD)
Rastas's has been given a number n. Being weak at mathematics, she has to consider all the numbers f ...
- 「LuoguP1281」 书的复制(贪心
Description 大多数人的错误原因:尽可能让前面的人少抄写,如果前几个人可以不写则不写,对应的人输出0 0. 不过,已经修改数据,保证每个人都有活可干. // 现在要把m本有顺序的书分给k给人 ...