题目如下:

解题思路:这个题目可以这么做,遍历数组,如果元素是0,则count --;否则count ++;这样的话,每遍历到一个下标i,count的值就是0>i区间内0和1的差值。如果我们能找到在0->i-1区间内差值也为count的最小下标j,那么j->i区间就是一个0和1数量相同的区间。遍历过程中记录最大的j->i长度即可得到答案。

代码如下:

class Solution(object):
def findMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dic = {}
count = 0
res = 0
for i,v in enumerate(nums):
if v == 0:
count -= 1
else:
count += 1
if count == 0: # count = 0 表示0->i 区间内0和1数量相同,肯定是当前的最大长度
res = i+1
continue
if dic.has_key(count):
res = max(res,i - dic[count]) #找到历史出现的count的最小下标,差值即为一个符合条件的区间的长度
if dic.has_key(count) == False: #记录每个count出现的最小下标
dic[count] = i
#print dic
return res

【leetcode】525. Contiguous Array的更多相关文章

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

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

  2. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  3. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  4. 【LeetCode】915. Partition Array into Disjoint Intervals 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/partitio ...

  5. 【leetcode】1043. Partition Array for Maximum Sum

    题目如下: Given an integer array A, you partition the array into (contiguous) subarrays of length at mos ...

  6. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

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

  7. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  8. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

  9. 【LeetCode】922. Sort Array By Parity II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用奇偶数组 排序 奇偶数位置变量 日期 题目地址: ...

随机推荐

  1. Delphi加密解密算法

    // 加密方法一(通过密钥加密解密)function EncryptString(Source, Key: string): string;function UnEncryptString(Sourc ...

  2. OutLook会议室预定提醒

    项目组采用敏捷开发管理,每两周一个迭代.写个工具做会议室预定. 代码下载:https://download.csdn.net/download/linmilove/10547579 Appointme ...

  3. 第三章 四大组件之Activity(一)生命周期

    1.生命周期: onCreate()->onStart()->onResume()->onPause()->onStop()->onDestroy() 2.各种状况下Ac ...

  4. Maven 中 resources 作用

    默认情况下,如果没有指定resources,目前认为自动会将src/main/resources下的.xml文件放到target里头的classes文件夹下的package下的文件夹里.如果设定了re ...

  5. linux下安装msgpack,yar,phalcon

    安装msgpack扩展 下载:http://pecl.php.net/package/msgpack cd /usr/local tar zxvf msgpack-0.5.5.tgz cd msgpa ...

  6. CSUST 8.3 早训

    A - Settlers' Training CodeForces - 63B 题意 给你一串数字,相同的数字为一组,每次可以给一组中的一个数字加一,问这一串数字全变成K需要多少步? 题解 模拟 C+ ...

  7. 行内元素(例如)设置float之后才能用width调整宽度

    因为只有块元素才会有物理属性,在css世界里边,有三种形态的东西, 1. 块元素. 特性:有物理属性,width,height写值起作用,而且要占据一行.2. 内联元素. 特性:没有物理属性.但是ma ...

  8. Vue下简单分页及搜索功能

    最近利用Vue和element ui仿写了个小页面,记一哈分页和搜索功能的简单实现. 首页   emmmm..... 搜索框输入..... 搜索完成 数据是直接写在这里面的: cardPhoto:[ ...

  9. Macrotask Queue和Microtask Quque

    from:http://www.jianshu.com/p/3ed992529cfc setImmediate(function(){ console.log(1); },0); setTimeout ...

  10. 前端开发-css基础入门

    CSS  cascading(层叠) style(样式) sheet(表) css注释 /* 注释内容 */ 快捷键:ctrl ? 引入方式 <!-- 1.行间样式 --> <div ...