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 ...
随机推荐
- camus gobblin
####Camus is being phased out and replaced by Gobblin. For those using or interested in Camus, we su ...
- C# delegate Action<T> lambda表达式
转载以记录:http://blog.csdn.net/educast/article/details/7219854 在使用 Action<T> 委托时,不必显式定义一个封装只有一个参数的 ...
- 类、对象(java基础知识六)
1.Java约定俗成 java约定俗成 1,类名接口名 一个单词首字母大写,多个单词每个单词首字母都大写 2,方法名和变量名 一个单词全部小写,多个单词从第二个单词首字母大写 建议:如果能用英语尽量用 ...
- Javascript版五子棋
Javascript版五子棋,无禁手.欢迎提出算法的改进意见.2. [代码]HTML <!DOCTYPE html><html> <head> ...
- iOS在一个label中显示不同颜色的字体
UILabel *Label = [[UILabel alloc] initWithFrame:CGRectMake(20, 300, 300, 30)]; NSMutableAttributedSt ...
- Ubuntu bitnami gitlab 安装
/************************************************************************************** * Ubuntu bit ...
- 「LuoguP1145」 约瑟夫(打表
Description n 个人站成一圈,从某个人开始数数,每次数到 m 的人就被杀掉,然后下一个人重新开始数,直到最后只剩一个人.现在有一圈人, k 个好人站在一起, k 个坏人站在一起.从第一个好 ...
- NOIP提高组2006-金明的预算方案
链接 分析:依赖型0-1背包问题,对于一个主件,可以挂0个,1个,2个附件,所以最终为4种状态情况下的最大值. #include "iostream" #include " ...
- Python项目使用memcached缓存
前言许多Web应用都将数据保存到MySQL这样的关系型数据库管理系统中,应用服务器从中读取数据并在浏览器中显示. 但随着数据量的增大.访问的集中,就会出现数据库的负担加重.数据库响应恶化. 网站显示延 ...
- eclipse恢复界面默认设置
使用eclipse的时候有时候会一不小心把一些界面设置给弄乱,可以恢复默认界面设置 eclipse导航栏window选项卡 找到Perspective->点击Reset Perspective ...