525. 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.
Approach #1: C++.
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int ans = 0;
int size = nums.size();
unordered_map<int, int> prefix_sum; // store the first position where the sum first appeared.
int sum = 0;
for (int i = 0; i < size; ++i) {
sum += nums[i] ? 1 : -1;
if (sum == 0) ans = i+1;
else if (!prefix_sum.count(sum)) prefix_sum[sum] = i;
else ans = max(ans, i - prefix_sum[sum]);
}
return ans;
}
};
Approach #2: Java.
class Solution {
public int findMaxLength(int[] nums) {
for (int i = 0; i < nums.length; ++i) {
if (nums[i] == 0) nums[i] = -1;
}
Map<Integer, Integer> sumToIndex = new HashMap<>();
sumToIndex.put(0, -1);
int sum = 0, max = 0;
for (int i = 0; i < nums.length; ++i) {
sum += nums[i];
if (sumToIndex.containsKey(sum)) {
max = Math.max(max, i - sumToIndex.get(sum));
} else {
sumToIndex.put(sum, i);
}
}
return max;
}
}
Approach #3: Python.
class Solution(object):
def findMaxLength(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
count = 0
max_length = 0
table = {0 : 0}
for index, num in enumerate(nums, 1):
if num == 0:
count -= 1
else:
count += 1 if count in table:
max_length = max(max_length, index - table[count])
else:
table[count] = index return max_length
Analysis:
In this problem if we use double cycle traversing the vector to find the subarray, because the length of the given binary array will not exceed 50,000, it will Time Limt Exceeded.
So we can use a hash table to save time using space.
First, we have to make the elements in the nums which equal to 0 transform to -1.
Then we travel the array and calculate the prefix sum. if the sum don't count in hash map, we put sum into the hash table, else i - prefix_sum[sum] is a dummy answer which we want to find.
Having one thing we have to notice is which if the array is [0, 1], you may finding that the prefix sum never appeared in this case, so it will return 0 finally. but the really answer is 2. How can we solve this problem?
- In approach one we using a if statemen to judge if the answer equal to 0 we coulde make ans = i + 1 using this way we can solve the problem of initialization.
- In approach two we push the key and value {0, -1} in the first, this have the same effect with the first one.
525. Contiguous Array的更多相关文章
- LeetCode 525. Contiguous Array
525. Contiguous Array Add to List Description Submission Solutions Total Accepted: 2476 Total Submis ...
- [LeetCode] 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两位求和为1的对数
[抄题]: Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 ...
- 【LeetCode】525. Contiguous Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- 525 Contiguous Array 连续数组
给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组.示例 1:输入: [0,1]输出: 2说明: [0, 1] 是具有相同数量0和1的最长连续子数组. 示例 2:输入: [0,1, ...
- 【leetcode】525. Contiguous Array
题目如下: 解题思路:这个题目可以这么做,遍历数组,如果元素是0,则count --:否则count ++:这样的话,每遍历到一个下标i,count的值就是0>i区间内0和1的差值.如果我们能找 ...
- Contiguous Array with Equal Number of 0 & 1
2018-07-08 13:24:31 问题描述: 问题求解: 问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度.本题使用DP和滑动数组都比较棘手,这里给出的方案是p ...
- [LeetCode] 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. ...
随机推荐
- centos 6 7 differences 区别
命令 centos6 centos7 ifconfig 有 有 yum install -y net-tools 服务管理 chkconfig /etc/init.d/服务 systemctl sys ...
- Android笔记之自定义对话框
效果如下图 对话框布局 dialog_uninstallation_confirmation.xml <?xml version="1.0" encoding="u ...
- 时间操作(Java版)—获取给定日期N天后的日期
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/wangshuxuncom/article/details/34896777 获取给定 ...
- [Java多线程] volatile 关键字正确使用方法
volatile 变量具有 synchronized 的可见性特性,但是不具备原子特性,即多线程环境中,使用 volatile 关键字的变量仅可以保证不同线程读取变量时,可以读到最新修改的变量值,但是 ...
- px dp 互转
/** * * px dip 转换 */ public class DensityUtil { /** * 根据手机分辨率 dip 转 px */ static public int dip2px(C ...
- Mac查看端口
lsof -i tcp:port eg: lsof -i tcp:8899
- python基础知识踩点
1.注释 在python中,注释是以任何存在于#右侧的文字,其主要作用是写给程序读者看的笔记. 例如 单行注释 >>print("hello world") #这是一个 ...
- [NOIP2011提高组day1]-3-mayan游戏
3.Mayan 游戏 (mayan.cpp/c/pas) [问题描述] Mayan puzzle 是最近流行起来的一个游戏.游戏界面是一个 7行 5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即 ...
- ES6 对Number的扩展
Number.isFinite(), Number.isNaN() ES6 在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法. Number.is ...
- 为 Android 平台开发一个输入法
学习目标: 实现新的输入法 学习目的: 掌握Android输入法框架 学习收获: Android 1.5 新特色之一就是输入法框架(Input Method Framework,IMF),正是它的出现 ...