当前位置:网站首页>【无标题】
【无标题】
2022-07-06 09:18:00 【邓嘉文Jarvan】
[算法] 剑指offer2 golang 0和1个数相同的子数组
题目1:
思路1: 暴力
//思路1: 暴力
//将0当作-1看待,
//固定一个,计算后面的位数是否总和位0,计算最大的个数,
//O(n3),优化下计算时间复杂度O(n2)
代码1
func findMaxLength(nums []int) int {
//返回最长的子数组的长度
//start :19.11
//思路1: 暴力
//将0当作-1看待,
//固定一个,计算后面的位数是否总和位0,计算最大的个数,
//O(n3),优化下计算时间复杂度O(n2)
//参数处理
if len(nums) == 0 {
return 0
}
//暴力
max := 0
for i := 0; i < len(nums) - 1; i++ {
//左指针
sum := 1
if nums[i] == 0 {
sum = -1
}
for j := i+1; j < len(nums); j++{
if nums[j] == 0 {
sum--
}else {
sum++
}
// 符合条件刷新最大值 max
temp := j - i +1
if sum == 0 && temp > max{
max = temp
}
}
}
return max
}
测试1

思路2: hashmap
//1.使用 hashmap[sum]index 记录前 n 个元素和位 sum 的下标,如果后面的元素 sum2
//如果 sum2 - sum1 = 0, sum2 = sum1,证明存在就取之前的 value
代码2:
func findMaxLength(nums []int) int {
//返回最长的子数组的长度
//start :19.26
//思路2: hashmap
//1.使用 hashmap[sum]index 记录前 n 个元素和位 sum 的下标,如果后面的元素 sum2
//如果 sum2 - sum1 = 0, sum2 = sum1,证明存在就取之前的 value
if len(nums) == 0 {
return 0
}
//1.hashmap 赋值
hashmap := make(map[int]int)
//处理边界
hashmap[0] = -1
sum1,max := 0,0
for i := 0; i < len(nums); i++ {
if nums[i] == 0 {
sum1 --
}else {
sum1 ++
}
if val,ok := hashmap[sum1]; ok {
length := i - val
if length > max {
max = length
}
}else {
//只是保留小index的,大的不需要
hashmap[sum1] = i
}
}
return max
}

边栏推荐
- Meanings and differences of PV, UV, IP, VV, CV
- 第一人称视角的角色移动
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- MySQL replacement field part content
- Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
- rtklib单点定位spp使用抗差估计遇到的问题及解决
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- (一)R语言入门指南——数据分析的第一步
- Gateway fails to route according to the service name, and reports an error service unavailable, status=503
- ESP8266连接onenet(旧版MQTT方式)
猜你喜欢

Gravure sans fil Bluetooth sur micro - ordinateur à puce unique

Unity场景跳转及退出

The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25

Fairygui loop list

idea中好用的快捷键

Unity3D,阿里云服务器,平台配置

NRF24L01故障排查

编译原理:源程序的预处理及词法分析程序的设计与实现(含代码)

使用rtknavi进行RT-PPP测试

FairyGUI人物状态弹窗
随机推荐
【RTKLIB 2.4.3 b34 】版本更新简介一
KF UD分解之UD分解基础篇【1】
JS function promotion and declaration promotion of VaR variable
Mixed use of fairygui button dynamics
(4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
使用rtknavi进行RT-PPP测试
C programming exercise
idea中导包方法
Unity scene jump and exit
How to improve the deletion speed of sequential class containers?
341. Flatten nested list iterator
(1) Introduction Guide to R language - the first step of data analysis
Naive Bayesian theory derivation
Game 280 weekly
How to add music playback function to Arduino project
Combination of fairygui check box and progress bar
Unity场景跳转及退出
Derivation of logistic regression theory
Introduction to the daily practice column of the Blue Bridge Cup
Pytorch: tensor operation (I) contiguous