LeetCode 525. Contiguous Array
525. Contiguous Array
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的更多相关文章
- [LeetCode] 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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- 【leetcode】525. Contiguous Array
题目如下: 解题思路:这个题目可以这么做,遍历数组,如果元素是0,则count --:否则count ++:这样的话,每遍历到一个下标i,count的值就是0>i区间内0和1的差值.如果我们能找 ...
- 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. ...
- 525 Contiguous Array 连续数组
给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组.示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2:输入: [0,1, ...
- Contiguous Array with Equal Number of 0 & 1
2018-07-08 13:24:31 问题描述: 问题求解: 问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度.本题使用DP和滑动数组都比较棘手,这里给出的方案是p ...
- 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 ...
- [LeetCode] Contiguous Array 邻近数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
随机推荐
- HTML5游戏开发系列教程7(译)
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-7/ 今天我们将完成我们第一个完整的游戏--打砖块.这次教程中,将 ...
- ruby中的链式访问和方法嵌套
先看一道题,这道题是codewars上的一道题,我很早就看到了,但是不会写.等到又看到这道题的时候,我刚看完元编程那本书,觉得是可以搞定它的时候了.废话不多说,先看这道题,题目最开始是为JavaScr ...
- “凯易迅Calix”实习上机——求元音开头辅音结尾的子串
题目要求: 对于一个输入的字符串(26个小写字母组成),求出他的所有子串中元音开头,辅音结尾的子串.在这所有的子串中,输出第一个和最后一个. 例如aab,共有子串a,aa,aab,ab,b:但是满足元 ...
- iOS 动态调用方法
- (void)bugly { dispatch_async(dispatch_get_global_queue(0, 0), ^{ if (NSClassFromString(@"Bu ...
- zookeeper 监听事件 PathChildrenCacheListener
zookeeper 监听事件 PathChildrenCacheListener PathChildrenCacheListener一次父节点注册,监听每次子节点操作,不监听自身和查询. 1.测试类: ...
- 《Java入门第三季》第一章 异常与异常处理
Java异常简介 1.Java异常的体系结构.万恶之源Throwable以及它的两个大儿子Mr.Error(程序终结者)和Mr.Exception(有大量儿子,包括不受查的RuntimeExcepti ...
- 20145307第二次JAVA学习实验报告
20145307<Java程序设计>实验报告二:Java面向对象程序设计 实验要求 1.初步掌握单元测试和TDD 2.理解并掌握面向对象三要素:封装.继承.多态 3.初步掌握UML建模 4 ...
- 20145328 《Java程序设计》第3周学习总结
20145328 <Java程序设计>第3周学习总结 教材学习内容总结 第四章 对象Object:存在的具体实体,具有明确的状态和行为 类Class:具有相同属性和行为的一组对象的集合,用 ...
- 20145109 《Java程序设计》第九周学习总结
JDBC 1 . DriverManager Class.forName("oracle.jdbc.driver.OracleDriver").newInstance(); 2 . ...
- 【前端】vue.js实现输入框绑定
vue.js实现输入框绑定 实现效果如下: 实现代码及注释 <!DOCTYPE html> <html> <head> <title>vue.js数据动 ...