525. Contiguous Array两位求和为1的对数
[抄题]:
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.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为要用dp,求个数
结果求和类的问题还是要用hashmap
[一句话思路]:
0,1不好弄中间值,把0改成-1后再求中间值
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
0,1不好弄中间值,把0改成-1后再求中间值
[复杂度]:Time complexity: O(n) Space complexity: O(n)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int findMaxLength(int[] nums) {
//cc
if (nums == null || nums.length == 0) return 0;
//ini: hashmap, 1 to -1, sum
Map<Integer, Integer> map = new HashMap<>();
map.put(0, -1);
int sum = 0, max = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 0) nums[i] = -1;
}
//for loop, check sum
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
//contain or not
if (map.containsKey(sum)) {
max = Math.max(max, i - map.get(sum));
}else {
map.put(sum, i);
}
}
return max;
}
}
525. Contiguous Array两位求和为1的对数的更多相关文章
- 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. ...
- 【LeetCode】525. Contiguous Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 累积和 日期 题目地址:https://leetco ...
- 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, ...
- 【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 ...
- 关于Oracle中查询的数字值的显示格式需要保留小数点后两位(或者三位,及其他位数)
关于Oracle中查询的数字值的显示格式需要保留小数点后两位(或者三位,及其... 方法一:使用to_char的fm格式,即: to_char(round(data.amount,2),'FM9999 ...
- Oracle 之 保留两位小数
项目需要使用百分率,保留2位小数,只用 round 和 trunc 函数都可以实现(round(_data,2) ),只是格式不是很工整,对格式要求不严谨的情况下使用 round 即可. 以下是比较方 ...
随机推荐
- DB time VS. DB CPU
如何行之有效地展示系统负载在做系统调优的时候是必不可少的技巧.通常我们会使用Oracle提供的Time Model,比如我们需要作出类似于下面这样的趋势图来展示系统负载的高低. 这样的趋势图可以直接使 ...
- google adwords report相关类型
(来自enum的ReportDefinitionReportType) KEYWORDS_PERFORMANCE_REPORT, AD_PERFORMANCE_REPORT, URL_PE ...
- Android 从上层到底层-----jni层
CPU:RK3288 系统:Android 5.1 功能:上层 app 控制 led 亮灭 开发板:Firefly RK3288 led_jni.h path:hardware/rockchip/fi ...
- mysql设置合适的索引长度
理想的索引: 相对于写操作来说,表查询很频繁的表建立索引 字段区分度高 长度小(合适的长度,不是越小越好) 尽量能够覆盖常用字段 这些条件综合起来才能够达到最优索引,本次我们着重聊一下建立合适长度的索 ...
- eclipse和myeclipse的he user operation is wating问题
近做了一个MyEclipse项目,但是没开始多久就发现了这个问题:只要文件被修改过,不论多小的修改,保存的时候都会跳出一个框框,里面写着the user operation is wating.... ...
- [Java][Web]利用 referer 防盗链
String referer = request.getHeader("referer"); if(referer == null || !referer.startsWith(& ...
- Windows7下搭建Python2.7环境
机器: Windows7_x86_64 步骤: 1.下载Python2.7 下载地址:https://www.python.org/downloads/ 2.安装Python2.7 双击安装包,安装过 ...
- 编译hostapd时,出现错误:/usr/bin/ld: cannot find -lnl
book@ubuntu:/work/project/wifi/04.hostapd/hostapd-2.0/hostapd$ make /usr/bin/ld: cannot find -lnl co ...
- Java:类与继承(隐藏和覆盖的问题)
盒子先生金金 Java:类与继承(隐藏和覆盖的问题) Java:类与继承 Java:类与继承 对于面向对象的程序设计语言来说,类毫无疑问是其最重要的基础.抽象.封装.继承.多态这四大特性都离不 ...
- angularJs中的发送请求例子
$http({ //发送请求 url: 'http://localhost:8080/teacher/api/login', method: 'post', data: obj }) .success ...