525. Contiguous Array

Add to List

Description Submission Solutions

  • Total Accepted: 2476
  • Total Submissions: 8220
  • Difficulty: Medium
  • Contributors: bishwa

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

Example 1:

Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

Note: The length of the given binary array will not exceed 50,000.

Subscribe to see which companies asked this question.


【题目分析】

这个题目的意思是给定一个只包含0,1的整数数组,找到数组中最长的一个子序列,该序列中1的个数和0的个数相同。


【思路】

这个题目真的很有意思啊,我感觉自己想不到这么好的解法。

看大家的解题思路如下:

The idea is to change 0 in the original array to -1. Thus, if we find SUM[i, j] == 0 then we know there are even number of -1 and 1 between index i and j. Also put the sum to index mapping to a HashMap to make search faster.


【java代码】

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

LeetCode 525. Contiguous Array的更多相关文章

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

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

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

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

  3. 【leetcode】525. Contiguous Array

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

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

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

  5. 525. Contiguous Array

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

  6. 525 Contiguous Array 连续数组

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

  7. Contiguous Array with Equal Number of 0 & 1

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

  8. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  9. [LeetCode] Contiguous Array 邻近数组

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

随机推荐

  1. mysqldump 参数

    mysqldump是mysql用于转存储数据库的实用程序.它主要产生一个SQL脚本,其中包含从头重新创建数据库所必需的命令CREATE TABLE INSERT等.对于导出的文件,可使用SOURCE命 ...

  2. 关于手机适配中的rem的学习随笔

    githup 下载地址 :https://github.com/comjustforfun/remformobile adaptivejs利用rem解决移动端页面开发的自适应问题 页面模板初始化的时候 ...

  3. 在vue中使用express-mock搭建mock服务

    首先安装 nodemon ,如果是全局安装,那么所有的项目都可以使用mock服务 npm install nodemon 再安装express-mockjs npm i -D express-mock ...

  4. vue react angular对比

    1.数据绑定 1)vue 把一个普通对象传给Vued的data选项,Vue会遍历此对象的所有属性,并使用Object.defineProperty将这些属性全部转为getter/setter.Obje ...

  5. Delphi APP 開發入門(三)簡易計算機

    Delphi APP 開發入門(三)簡易計算機 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數:68 ...

  6. 1.MySQL必知必会之数据库基础

    下面这几个是几个关于数据库的关键字的概念,为后面的教程做基础的: 数据库:保存有组织的数据的容器(通常是一个文件或一组文件).   表: 某种特定类型数据的结构化清单.   模式:关于数据库和表的布局 ...

  7. PKU 1932 XYZZY(Floyd+Bellman||Spfa+Floyd)

    题目大意:原题链接 给你一张图,初始你在房间1,初始生命值为100,进入每个房间会加上那个房间的生命(可能为负),问是否能到达房间n.(要求进入每个房间后生命值都大于0) 解题思路: 解法一:Floy ...

  8. 乐观锁的一种实现方式——CAS

    在java里面,synchronized关键字就是一种悲观锁,因为在加上锁之后,只有当前线程可以操作变量,其他线程只有等待. CAS操作是一种乐观锁,它假设数据不会产生冲突,而是在提交的时候再进行版本 ...

  9. 关于Android RenderScript 的详细说明和一些实用文档

    http://www.cnblogs.com/TerryBlog/archive/2012/03/02/2377251.html RenderScript 是一种低级的高性能编程语言,用于3D渲染和处 ...

  10. linux 登录数据库

    -h   地址 (例如:127.0.0.1,localhost) mysql -h 127.0.0.1 -u root -p