leetcode 525. 連續數組

給定一個二進制數組 nums , 找到含有相同數量的 0 和 1 的最長連續子數組,并返回該子數組的長度。

示例 1:

輸入: nums = [0,1]
輸出: 2
說明: [0, 1] 是具有相同數量 0 和 1 的最長連續子數組。
示例 2:

輸入: nums = [0,1,0]
輸出: 2
說明: [0, 1] (或 [1, 0]) 是具有相同數量0和1的最長連續子數組。

解題思路

維護一個變量bi,存儲子數組[0,i]里面1和0的差值(1的數量-0的數量)

假設子數組為[i,j],若想具有相同數量 0 和 1

子數組中1的數量=子數組[0,j]中1的數量-子數組[0,i]中1的數量

子數組中0的數量=子數組[0,j]中0的數量-子數組[0,i]中0的數量

子數組中1的數量=子數組中0的數量

子數組[0,j]中1的數量-子數組[0,i]中1的數量=子數組[0,j]中0的數量-子數組[0,i]中0的數量

子數組[0,j]里面1和0的差值=子數組[0,i]里面1和0的差值

所以我們只需要找到相同的1和0的差值,就能判斷它們具有相同數量的 0 和 1

代碼

func findMaxLength(nums []int) (maxLength int) {max := func(a int, b int) int {if a > b {return a} else {return b}}m := map[int]int{}b:=0for i, num := range nums {if num==0{b--}else{b++}if b==0{maxLength=max(maxLength,i+1)m[b]=icontinue}index,has := m[b]if has{maxLength=max(maxLength,i-index)}else {m[b]=i}}return 
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390688.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390688.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390688.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

實踐作業2:黑盒測試實踐(小組作業)每日任務記錄1

會議時間:2017年11月24日20:00 – 20:30 會議地點:在線討論 主 持 人:王晨懿 參會人員:王晨懿、余晨晨、鄭錦波、楊瀟、侯歡、汪元 記 錄 人:楊瀟 會議議題:軟件測試課程作業-黑盒測試實踐的啟動計劃 會議內…

視圖可視化 后臺_如何在單視圖中可視化復雜的多層主題

視圖可視化 后臺Sometimes a dataset can tell many stories. Trying to show them all in a single visualization is great, but can be too much of a good thing. How do you avoid information overload without oversimplification?有時數據集可以講述許多故事。 試圖在…

iam身份驗證以及訪問控制_如何將受限訪問IAM用戶添加到EKS群集

iam身份驗證以及訪問控制介紹 (Introduction) Elastic Kubernetes Service (EKS) is the fully managed Kubernetes service from AWS. It is deeply integrated with many AWS services, such as AWS Identity and Access Management (IAM) (for authentication to the cluste…

一步一步構建自己的管理系統①

2019獨角獸企業重金招聘Python工程師標準>>> 系統肯定要先選一個基礎框架。 還算比較熟悉Spring. 就選Spring boot postgres mybatis. 前端用Angular. 開始搭開發環境,開在window上整的。 到時候再放到服務器上。 自己也去整了個小服務器,…

面向對象面向過程

1、面向語句: 直接寫原生的sql語句,但是這樣代碼不容易維護。改一個方法會導致整個項目都要改動, 2、面向過程 定義一些函數,用的時候就調用不用就不調用。但是這也有解決不了的問題,如果要維護需要改動代碼&#xff0…

python邊玩邊學_邊聽邊學數據科學

python邊玩邊學Podcasts are a fun way to learn new stuff about the topics you like. Podcast hosts have to find a way to explain complex ideas in simple terms because no one would understand them otherwise 🙂 In this article I present a few episod…

react css多個變量_如何使用CSS變量和React上下文創建主題引擎

react css多個變量CSS variables are really cool. You can use them for a lot of things, like applying themes in your application with ease. CSS變量真的很棒。 您可以將它們用于很多事情,例如輕松地在應用程序中應用主題。 In this tutorial Ill show you …

vue 自定義 移動端篩選條件

1.創建組件 components/FilterBar/FilterBar.vue <template><div class"filterbar" :style"{top: top px}"><div class"container"><div class"row"><divclass"col":class"{selected: ind…

PSP

姓名&#xff1a;袁亞琴 日期&#xff1a;11月27日 教師&#xff1a;王建民 課程&#xff1a;PSP 項目計劃日志&#xff1a; PSP Planning . Estimate Development . Analysis . Design Spec . Design Review . …

如何在Windows中打開和使用命令提示符

入門 (Getting started) Windows, MacOS and Linux have command line interfaces. Windows’ default command line is the command prompt. The command prompt allows users to use their computer without pointing and clicking with a mouse. Windows&#xff0c;MacOS和…

ACM-ICPC北京賽區2017網絡同步賽H

http://hihocoder.com/contest/icpcbeijing2017/problem/8 預處理暴力枚舉修改的點 #include <bits/stdc.h> using namespace std; const int maxn 159; const int inf 0x3f3f3f3f; int a[maxn][maxn]; int colsum[maxn][maxn]; int rowsum[maxn][maxn]; int dp[maxn];…

PPPOE撥號上網流程及密碼竊取具體實現

樓主學生黨一枚&#xff0c;最近研究netkeeper有些許心得。 關于netkeeper是調用windows的rasdial來進行上網的東西&#xff0c;網上已經有一大堆&#xff0c;我就不贅述了。 本文主要講解rasdial的部分核心過程&#xff0c;以及我們可以利用它來干些什么。 netkeeper中rasdial…

leetcode 160. 相交鏈表(雙指針)

給你兩個單鏈表的頭節點 headA 和 headB &#xff0c;請你找出并返回兩個單鏈表相交的起始節點。如果兩個鏈表沒有交點&#xff0c;返回 null 。 圖示兩個鏈表在節點 c1 開始相交&#xff1a; 題目數據 保證 整個鏈式結構中不存在環。 注意&#xff0c;函數返回結果后&#…

android開發入門_Android開發入門

android開發入門Android is an open source, Linux-based mobile operating system. Android was developed by the Open Handset Alliance, which was lead by Google and featured contributions from many other companies.Android是基于Linux的開放源代碼移動操作系統。 An…

新購阿里云服務器ECS創建之后無法ssh連接的問題處理

作者&#xff1a;13 GitHub&#xff1a;https://github.com/ZHENFENG13 版權聲明&#xff1a;本文為原創文章&#xff0c;未經允許不得轉載。 問題描述 由于原服務器將要到期&#xff0c;因此趁著阿里云搞促銷活動重新購買了一臺ECS服務器&#xff0c;但是在初始化并啟動后卻無…

數據下發非標準用戶權限測試

與同事一起溝通了下MDM的Oracle權限部分: create user cx default tablespace cwbaseoe73 identified by Test6530 grant select,update,delete,insert on lcoe739999.lsbzdw to cx grant create table to cx alter user cx quota unlimited on cwbaseoe73 grant create sessio…

leetcode 474. 一和零(dp)

給你一個二進制字符串數組 strs 和兩個整數 m 和 n 。 請你找出并返回 strs 的最大子集的大小&#xff0c;該子集中 最多 有 m 個 0 和 n 個 1 。 如果 x 的所有元素也是 y 的元素&#xff0c;集合 x 是集合 y 的 子集 。 示例 1&#xff1a; 輸入&#xff1a;strs [“10”…

邊緣計算 ai_在邊緣探索AI!

邊緣計算 ai介紹 (Introduction) What is Edge (or Fog) Computing?什么是邊緣(或霧)計算&#xff1f; Gartner defines edge computing as: “a part of a distributed computing topology in which information processing is located close to the edge — where things a…

JavaScript中的全局變量介紹

Global variables are declared outside of a function for accessibility throughout the program, while local variables are stored within a function using var for use only within that function’s scope. If you declare a variable without using var, even if it’…

初識spring-boot

使用Spring或者SpringMVC的話依然有許多東西需要我們進行配置&#xff0c;這樣不僅徒增工作量而且在跨平臺部署時容易出問題。 使用Spring Boot可以讓我們快速創建一個基于Spring的項目&#xff0c;而讓這個Spring項目跑起來我們只需要很少的配置就可以了。Spring Boot主要有如…