[LeetCode] Contiguous Array 邻近数组
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.
这道题给了我们一个二进制的数组,让找邻近的子数组使其0和1的个数相等。对于求子数组的问题,需要时刻记着求累积和是一种很犀利的工具,但是这里怎么将子数组的和跟0和1的个数之间产生联系呢?这里需要用到一个 trick,遇到1就加1,遇到0,就减1,这样如果某个子数组和为0,就说明0和1的个数相等,这个想法真是太叼了,不过博主木有想出来。知道了这一点,就用一个 HashMap 建立子数组之和跟结尾位置的坐标之间的映射。如果某个子数组之和在 HashMap 里存在了,说明当前子数组减去 HashMap 中存的那个子数字,得到的结果是中间一段子数组之和,必然为0,说明0和1的个数相等,更新结果 res。注意这里需要在 HashMap 初始化一个 0 -> -1 的映射,这是为了当 sum 第一次出现0的时候,即这个子数组是从原数组的起始位置开始,需要计算这个子数组的长度,而不是建立当前子数组之和 sum 和其结束位置之间的映射。比如就拿例子1来说,nums = [0, 1],当遍历0的时候,sum = -1,此时建立 -1 -> 0 的映射,当遍历到1的时候,此时 sum = 0 了,若 HashMap 中没有初始化一个 0 -> -1 的映射,此时会建立 0 -> 1 的映射,而不是去更新这个满足题意的子数组的长度,所以要这么初始化,参见代码如下:
解法一:
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int res = , n = nums.size(), sum = ;
unordered_map<int, int> m{{, -}};
for (int i = ; i < n; ++i) {
sum += (nums[i] == ) ? : -;
if (m.count(sum)) {
res = max(res, i - m[sum]);
} else {
m[sum] = i;
}
}
return res;
}
};
下面这种方法跟上面的解法基本上完全一样,只不过在求累积和的时候没有用条件判断,而是用了一个很叼的等式直接包括了两种情况,参见代码如下:
解法二:
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int res = , n = nums.size(), sum = ;
unordered_map<int, int> m{{, -}};
for (int i = ; i < n; ++i) {
sum += (nums[i] << ) -;
if (m.count(sum)) {
res = max(res, i - m[sum]);
} else {
m[sum] = i;
}
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/525
类似题目:
Maximum Size Subarray Sum Equals k
参考资料:
https://leetcode.com/problems/contiguous-array/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Contiguous Array 邻近数组的更多相关文章
- 994.Contiguous Array 邻近数组
描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...
- [LeetCode] Patching Array 补丁数组
Given a sorted positive integer array nums and an integer n, add/patch elements to the array such th ...
- [LeetCode] Rotate Array 旋转数组
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- [LeetCode] 565. Array Nesting 数组嵌套
A zero-indexed array A of length N contains all integers from 0 to N-1. Find and return the longest ...
- LeetCode Rotate Array 翻转数组
题意:给定一个数组,将该数组的后k位移动到前n-k位之前.(本题在编程珠玑中第二章有讲) 思路: 方法一:将后K位用vector容器装起来,再移动前n-k位到后面,再将容器内k位插到前面. class ...
- 525 Contiguous Array 连续数组
给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组.示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2:输入: [0,1, ...
- LeetCode 525. Contiguous Array
525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...
- Contiguous Array with Equal Number of 0 & 1
2018-07-08 13:24:31 问题描述: 问题求解: 问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度.本题使用DP和滑动数组都比较棘手,这里给出的方案是p ...
- C++ STL@ list 应用 (leetcode: Rotate Array)
STL中的list就是一双向链表,可高效地进行插入删除元素. List 是 C++标准程式库 中的一个 类 ,可以简单视之为双向 连结串行 ,以线性列的方式管理物件集合.list 的特色是在集合的任何 ...
随机推荐
- mysql的备份脚本
mysql的备份脚本 脚本如下 #!/bin/sh # mysql_backup.sh: backup mysql databases and keep newest 5 days backup. # ...
- SQL中哪些情况会引起全表扫描
1.模糊查询效率很低:原因:like本身效率就比较低,应该尽量避免查询条件使用like:对于like '%...%'(全模糊)这样的条件,是无法使用索引的,全表扫描自然效率很低:另外,由于匹配算法的关 ...
- 个人总结——Beta阶段
Beta总结 我们在beta 结束之后, 每位写一个博客, 回顾并总结自己的beta过程,哪些方面做的好的,哪些方面做得不足需要改进的 回答问题 分析在Alpha阶段自己提出的五个问题,针对每个问题, ...
- Beta 第四天
今天遇到的困难: 百度位置假死的问题研究发现并不是源于代码的问题,而是直接运行在主线程中会出现诸多问题 Fragment碎片刷新时总产生的固定位置的问题未果 今天完成的任务: 陈甘霖:修复了部分Bug ...
- bug终结者 团队作业第六、七周
bug终结者 团队作业第六.七周 作业要求:团队作业第六.七周 博客编辑:20162322 朱娅霖 一.修改<需求规格说明书> <需求规格说明书>2.0版(即初稿) <需 ...
- iOS开发-OC分支结构
BOOL类型 返回值:真:YES 假:NO BOOL数据类型占一个字节的内存空间,占位符为%d. 计算机在识别时,YES就替换成1,NO就替换成0. bool是C语言中的布尔类型,返回值为true ...
- Java中RuntimeException和Exception的区别
[TOC] 1. 引入RuntimeException public class RuntimeException { public static void main(String[] args) { ...
- Junit 4 测试中使用定时任务操作
难度:测试中执行线程操作 package com.hfepc.job.dataCollection.test; import java.util.Date; import java.util.List ...
- MySQL InnoDB锁机制
概述: 锁机制在程序中是最常用的机制之一,当一个程序需要多线程并行访问同一资源时,为了避免一致性问题,通常采用锁机制来处理.在数据库的操作中也有相同的问题,当两个线程同时对一条数据进行操作,为了保证数 ...
- 虚拟机Vmware成功安装Ubuntu Server 16.04中文版
最近想在Linux下学习Python的爬虫开发技术,经过认真考虑优先选择在在Ubuntu环境下进行学习Python的开发,虽然Ubuntu Server 16.04 LTS版本已经集成了Python ...