525 Contiguous Array 连续数组
给定一个二进制数组, 找到含有相同数量的 0 和 1 的最长连续子数组。
示例 1:
输入: [0,1]
输出: 2
说明: [0, 1] 是具有相同数量0和1的最长连续子数组。
示例 2:
输入: [0,1,0]
输出: 2
说明: [0, 1] (或 [1, 0]) 是具有相同数量0和1的最长连续子数组。
注意: 给定的二进制数组的长度不会超过50000。
详见:https://leetcode.com/problems/contiguous-array/description/
Java实现:
class Solution {
public int findMaxLength(int[] nums) {
int res=0;
int n=nums.length;
int sum=0;
Map<Integer,Integer> m=new HashMap<Integer,Integer>();
m.put(0,-1);
for(int i=0;i<n;++i){
sum+=(nums[i]==1)?1:-1;
if(m.containsKey(sum)){
res=Math.max(res,(i-m.get(sum)));
}else{
m.put(sum,i);
}
}
return res;
}
}
C++实现:
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;
}
};
参考:http://www.cnblogs.com/grandyang/p/6529857.html
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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- [LeetCode] 525. Contiguous Array 相连的数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- [LeetCode] Contiguous Array 邻近数组
Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1. ...
- 994.Contiguous Array 邻近数组
描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...
- 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. ...
- 【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 ...
随机推荐
- Xcode6.1 Prefix.pch添加方式
本文转载:http://blog.csdn.net/foolsong/article/details/40653497 在Xcode6.1中创建工程默认是没有Prefix.pch文件的,需要 ...
- HDU 6125 Free from square 状态压缩DP + 分组背包
Free from square Problem Description There is a set including all positive integers that are not mor ...
- log Configuration
Log4j – Configuring Log4j 2 - Apache Log4j 2 https://logging.apache.org/log4j/2.x/manual/configurati ...
- springboot实现定时任务的两种方式
方式一:在springboot启动类上添加@EnableScheduling注解,然后创建具体的任务类,在方法上添加@Scheduled注解,并指明执行频率即可.如下: @Componentpubli ...
- CodeChef:Little Elephant and Colored Coins
类似墨墨的等式 设f[2][j][k]表示a[i].c是否和当前颜色相同,到当前枚举到的颜色为止,颜色数为j,对mnv取模为k的最小数 这是个无限循环背包,用spfa优化 #include<cs ...
- YTU 2878: 结构体--学生信息排序
2878: 结构体--学生信息排序 时间限制: 1 Sec 内存限制: 128 MB 提交: 297 解决: 148 题目描述 定义存放一个学生信息的结构体类型,学生信息包括:姓名,学号,性别,院 ...
- hdu-5676 ztr loves lucky numbers(乱搞题)
题目链接: ztr loves lucky numbers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ( ...
- 汇编环境的搭建(windows 10 + debug)
1. debug.exe 安装 win10 版本过高,不再提供 debug.exe,甚至从别处获取的 debug.exe 的也无法运行. 汇编语言学习所需的各种执行文件(debug.exe.link. ...
- 计算机科学 —— 时间戳(timestamp)
时间戳的一个重要属性即是:唯一性,以起到唯一标识的作用: 1. linux 命令行 $ date +%s 1506222745 2. Python 时间戳 内置 time 库 >> tim ...
- SPOJ:NO GCD (求集合&秒啊)
You are given N(1<=N<=100000) integers. Each integer is square free(meaning it has no divisor ...