Lintcode 994 · Contiguous Array (presum + hashmap好题

2023-12-03 09:15

本文主要是介绍Lintcode 994 · Contiguous Array (presum + hashmap好题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

994 · Contiguous Array
Algorithms

The length of the given binary array will not exceed 50,000.

Example
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

这题应该是不能用滑动窗口做的。我试了很久,发现不行。不知道有没有高人能用滑动窗口做出来。

解法1:用presum + hashmap

class Solution {
public:/*** @param nums: a binary array* @return: the maximum length of a contiguous subarray*/int findMaxLength(vector<int> &nums) {int n = nums.size();if (n == 0) return 0;unordered_map<int, int> um; //<presum, pos>vector<int> presums(n + 1, 0);int res = 0;um[0] = 0;for (int i = 1; i <= n; i++) {presums[i] = presums[i - 1] + (nums[i - 1] == 0 ? -1 : 1);if (um.find(presums[i]) == um.end()) {um[presums[i]] = i;} else {res = max(res, i - um[presums[i]]);}}return res;}
};

这篇关于Lintcode 994 · Contiguous Array (presum + hashmap好题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/448905

相关文章

好题——hdu2522(小数问题:求1/n的第一个循环节)

好喜欢这题,第一次做小数问题,一开始真心没思路,然后参考了网上的一些资料。 知识点***********************************无限不循环小数即无理数,不能写作两整数之比*****************************(一开始没想到,小学没学好) 此题1/n肯定是一个有限循环小数,了解这些后就能做此题了。 按照除法的机制,用一个函数表示出来就可以了,代码如下

HashMap中常用的函数

假设如下HashMap<String, Integer> map = new HashMap<>();获取value值1、返回key为a的valueget(a)2、返回key为a的value,若没有该key返回0getOrDefault(a,0)新增键值对1、新增键值对(a,1)put(a,1)2、如果key为a的键不存在,则存入键值对(a,1)putIfAbsent(a,1)3

hashmap的存值,各种遍历方法

package com.jefflee;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class HashmapTest {// 遍历Hashmap的四种方法public static void main(String[] args) {//hashmap可以存一个null,把

【uva】11536-Smallest Sub-Array(区间移动问题)

一个区间移动的问题,1A了,感觉没什么好说的。。 13975926 11536 Smallest Sub-Array Accepted C++ 0.809 2014-08-01 11:00:20 #include<cstdio>#include<cstring>#include<iostream>using namespace std;#define INF 1 << 30

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

做一个问卷考试,标准答案对比用户填写的答案,array_diff 进行差集比对

if( empty(array_diff($answer_mark, $answer)) && empty(array_diff( $answer,$answer_mark))){//用户答题正确}else{// 答题错误} 做一个问卷考试,标准答案对比用户填写的答案,array_diff  进行差集比对   如用户填写的答案变量为answer   标准答案为answer_mark

容器第五课,Map和HashMap的基本用法,hashMap和HashTable的区别

Map接口 实现Map接口的类用来存储键(Key) --- 值(value)对。 Map接口的实现类有HashMap和TreeMap类 Map类中存储的键---值对通过键来标识,所以键值不能重复 package com.pkushutong.Collection;import java.util.HashMap;import java.util.Map;/*** 测试Map的基本用法

面试:HashMap

文章引用: http://www.importnew.com/7099.html https://blog.csdn.net/niuwei22007/article/details/52005329 HashMap的工作原理是近年来常见的Java面试题。几乎每个Java程序员都知道HashMap,都知道哪里要用HashMap,知道Hashtable和HashMap之间的区别,那么

HashMap 源码分析(删除+总结)JDK1.8

文章来源: 1 https://segmentfault.com/a/1190000012926722#articleHeader7 2 https://www.zhihu.com/question/20733617 3 https://tech.meituan.com/java-hashmap.html 4 https://www.zhihu.com/question/5752

【HashMap】深入原理解析

【HashMap】深入原理解析 分类: 数据结构 自考 equals与“==”(可以参考自己的另一篇博文)   1,基本数据类型(byte,short,char,int,long,float,double,boolean) 使用“==” 对比的是具体的值是否相等 2,复合数据类型 “== ”对比的是内存中存放的地址 object中的equals初始行为是比较内存中的地址,但在