994.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个数相等的子数组的长度。
这里用到一个sum,遇到1就加1,遇到0就减1,这样就会得到每个角标下的sum。
哈希表建立sum值和角标之间的映射。
遍历num成员计算sum值,如果哈希表中存在该sum值,就用当前角标减去哈希表中sum对应的角标,就会得到中间子数组长度,比较更新res。如果哈希表中不存在则添加该sum值和对应的角标。
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int res = 0, n = nums.size(), sum = 0;
unordered_map<int, int> m{{0, -1}};
for (int i = 0; i < n; ++i) {
sum += (nums[i] == 1) ? 1 : -1;
if (m.count(sum)) {
res = max(res, i - m[sum]);
} else {
m[sum] = i;
}
}
return res;
}
};
994.Contiguous Array 邻近数组的更多相关文章
- [LeetCode] 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 525. Contiguous Array
525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...
- golang之 Array(数组)
目录 一.Array(数组) 二.数组的定义 1. 基本语法 三.数组的初始化 1. 方式一 2. 方式二 3. 方式三 四.数组的遍历 1. 方式一:for循环遍历 2. 方式二:for range ...
- vector以及array和数组
//比较数组.vector.array #include <iostream> #include <vector> #include <array> #includ ...
- java中Array(数组)的用法
8.Array(数组) 数组是作为对象来实现的.(really occupy the memopry,真实的占用内存 ) An array is a data structure that st ...
- [LeetCode] 525. Contiguous Array 相连的数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- [Swift]LeetCode525. 连续数组 | Contiguous Array
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
随机推荐
- 【Teradata】配置PE和AMP(congfig和reconfig工具、vprocmanager)
The Reconfiguration and Configuration utilities are used to define the AMPs and PEs that operate tog ...
- 【技术与商业案例解读笔记】095:Google大数据三驾马车笔记
1.谷歌三驾马车地位 [关键词]开启时代,指明方向 聊起大数据,我们通常言必称谷歌,谷歌有“三驾马车”:谷歌文件系统(GFS).MapReduce和BigTable.谷歌的“三驾马车”开启了大数据时 ...
- jvisualVM的使用
jvisualvm能干什么:监控内存泄露,跟踪垃圾回收,执行时内存.cpu分析,线程分析... jvisualvm已经被集成在jdk1.6以上的版本中(不是jre).自身运行需要最低jdk1.6版本, ...
- 查看linux内存使用情况
查看内存使用情况 free -m total used free shared buffers cached Mem: -/+ buffers/cache: Swap: used=total-free ...
- windows下python2和python3共享
其实最好的解决方案使用 virtualenv http://pythonguidecn.readthedocs.io/zh/latest/dev/virtualenvs.html 不太好的方法,先安装 ...
- 机器学习之MCMC算法
1.MCMC概述 从名字我们可以看出,MCMC由两个MC组成,即蒙特卡罗方法(Monte Carlo Simulation,简称MC)和马尔科夫链(Markov Chain ,也简称MC).之前已经介 ...
- python入门学习:6.用户输入和while循环
python入门学习:6.用户输入和while循环 关键点:输入.while循环 6.1 函数input()工作原理6.2 while循环简介6.3 使用while循环处理字典和列表 6.1 函数in ...
- javascript中数组化的一般见解
javascript中数组化的一般见解,数组化浏览器中存在许多类数组对象,往往对类数组操作比较麻烦,没有数组那些非常方便的方法,在这种情况下,就有了数组化方法. 数组化的一般方法 1.第一种也是我们最 ...
- 初学Python——第一节课
一.Python语言的特性: 1.与C语言不同,Python语言是一门解释性语言.程序在执行过程中,执行一步.编译一步. 2.Python是一个动态类型语言,不需要定义变量的数据类型. 3.Pytho ...
- burnside+polya 整理
先定义几个含义和符号:起始状态/方法/位置/元素/:以染色为例,起始状态是所有的染色方案,方法是以起始状态所有染色方案为基准转变为新的染色情景的操作(如旋转),位置则必须是没有任何染色效果的抽象空间, ...