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 即可. 以下是比较方 ...
随机推荐
- Django ORM哪些操作
一般操作 看专业的官网文档,做专业的程序员! 必知必会13条 <1> all(): 查询所有结果 <2> filter(**kwargs): 它包含了与所给筛选条件相匹配的对象 ...
- 原生 Javascript 编写五子棋
原文地址:原生 Javascript 编写五子棋 博客地址:http://www.extlight.com 一.背景 近一个月没写 Javascript 代码,有点生疏.正好浏览网页时弹出五子棋的游戏 ...
- JavaFX 之自定义窗口标题栏(二)
一.问题场景 PC客户端登录界面仿QQ,上边显示图片,下边显示输入框和登录按钮.而JavaFX默认的窗口,不满足需求. 二.解决思路 隐藏窗口默认的标题栏,使用创建label对象,使用css将按钮图片 ...
- Unit08: Spring集成mybatis
Unit08: Spring集成mybatis 1. Spring集成mybatis (1)方式一 step1. 导包. spring-webmvc,mybatis,mybatis-spring, o ...
- VC编译选项 md /mdd /ml /mt/mtd
VC编译选项 多线程(/MT)多线程调试(/MTd)多线程 DLL (/MD)多线程调试 DLL (/MDd)C 运行时库 库文件Single threa ...
- Spring入门一----HelloWorld
知识点: 简介 HelloWorld 简介: 百度百科 HelloWorld 项目结构图: 导入Spring支持包: 然后选中所有包,右键Build Path à Add to Buil ...
- GOF23设计模式之策略模式(strategy)
一.策略模式概述 策略模式对应于解决某一个问题的一个算法族,允许用户从该算法族中任选一种算法解决一个问题,同时可以方便的更换算法或者增加新的算法.并且由客户端决定调用哪个算法. 策略模式的本质: 分离 ...
- 关于使用PyExecJS+nodejs使用与js反混淆
来源:https://cuiqingcai.com/5024.html 梳理这篇博客的时候出问题,我默认的是jscript作为pyexcJs的引擎,问题很大,大部分的js都无法加载,各种包用不了,只能 ...
- (转)CentOS 7安装Zabbix 3.4
(转)Zabbix 3.4 支持Centos 7.貌似不支持6.9. 更多详细内容请参考官方说明文档,详细的安装要求不贴出来了. https://www.zabbix.com/documentatio ...
- linux 下java环境的配置
注意:这里选择下载jdk并自行安装,而不是通过源直接安装(apt-get install) 1.下载jkd( http://www.oracle.com/technetwork/java/javase ...