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. PAT 1140 Look-and-say Sequence [比较]

    1140 Look-and-say Sequence (20 分) Look-and-say sequence is a sequence of integers as the following: ...

  2. 在Windows控制台应用程序中使用CString

    CString是在windows平台下开发中经常使用的字符串类, CString已从MFC中剥离出来了,可以单独使用,只需引用atlstr.h头文件即可. include "stdafx.h ...

  3. Selenium之使用PageFactory初始化pageobject

    使用PageFactory初始化pageobject有什么作用呢,下面举个例子来说明 public BaiduPage baiduPage = PageFactory.initElements(dri ...

  4. kubenetes 应用更新

    一.Deployment类型: 1.更新: 1).命令方式更新镜像: kubectl set image deployment nginx-deployment nginx=nginx:1.9.1 k ...

  5. 404 Not Found 探秘Nginx转发处理流程

    一.问题描述 访问一个链接地址后报404 Not Found nginx/1.10.2 1 112.95.211.154 - - [08/Mar/2018:15:22:21 +0800] " ...

  6. Connecting to a Remote Serial Port over TCP/IP

    https://www.acmesystems.it/socat This article illustrates how to realize a lan to serial gateway Rem ...

  7. Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) B. Code For 1

    地址:http://codeforces.com/contest/768/problem/B 题目: B. Code For 1 time limit per test 2 seconds memor ...

  8. SQL注入漏洞有哪些

    SQL注入攻击是当今最危险.最普遍的基于Web的攻击之一.所谓注入攻击,是攻击者把SQL命令插入到Web表单的输入域页面请求的查询字符串中,如果要对一个网站进行SQL注入攻击,首先需要找到存在SQL注 ...

  9. Ubuntu安装samba实现文件夹共享

    因工作需要,准备在Ubuntu服务器上共享文件夹,从Windows和Mac上都可以访问共享文件. 搜了一下,决定通过samba服务实现.安装过程如下: [step1]安装samba服务 sudo ap ...

  10. 清除list或者map集合,减少内存的占用率

    1.在编写程序对数据处理的时候我们经常会使用到list和map集合,有些时候我们存到集合里的数据只是暂时性的,在验证完或者使用完之后尽量对list或者map清空,及list.clear()后者map. ...