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 ...
 
随机推荐
- 2016/05/25   抽象类与API(接口)差别
			
简单来说, 接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的, 另外,实现接口的一定要实现接口里定义的所有方法,而实现抽象类可以有选择地重写需要用到的 ...
 - Preference+PreferenceArray+DataModel
			
在Mahout中,用户的喜好被抽象为一个Preference,包含了userId,itemId和偏好值(user对item的偏好).Preference是一个接口,它有一个通用的实现是GenericP ...
 - 为什么java web项目中要使用spring
			
1 不使用spring的理由 spring太复杂,不利于调试. spring太复杂,不利于全面掌控代码. spring加载bean太慢. 等等. 2 对不使用spring理由的辩驳 spring io ...
 - iframe 框架中 父子界面的JS调用
			
子界面调用父界面 window.parent.hello(); 父界面调用子界面 window.frmaes[i].hello();
 - springboot和redis处理页面缓存
			
页面缓存是应对高并发的一个比较常见的方案,当请求页面的时候,会先查询redis缓存中是否存在,若存在则直接从缓存中返回页面,否则会通过代码逻辑去渲染页面,并将渲染后的页面缓存到redis中,然后返回. ...
 - POJ 3750 小孩报数问题 (线性表思想 约瑟夫问题 数组模拟运算的 没用循环链表,控制好下标的指向就很容易了)
			
小孩报数问题 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10423 Accepted: 4824 Descripti ...
 - 一步一步学Silverlight 2系列(24):与浏览器交互相关辅助方法
			
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
 - Linux:外网域名防火墙设置导致下载失败
			
问题现象: 通过IE从服务器下载文件时,提示Can't read from connection: Connection reset by peer. 别的现场都是好的,只有该现场有这个问题.所以,一 ...
 - 查看JVM运行时堆内存
			
利用jmap和MAT等工具查看JVM运行时堆内存 https://www.cnblogs.com/cjsblog/p/9561375.html jmap JDK自带了一些工具可以帮助我们查看JVM运行 ...
 - NOIP2007普及 守望者的逃离
			
传送门 普及组的题目……很水. 原来写了一个模拟不过好像状态考虑的不全得了80,这次我们考虑一下dp做法. 守卫者有两种移动的方法,一种是闪现,一种是跑,我们可以把闪现和跑分开处理. 首先只处理闪现的 ...