当前位置:网站首页>【无标题】
【无标题】
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
}

边栏推荐
- MySQL時間、時區、自動填充0的問題
- Special palindromes of daily practice of Blue Bridge Cup
- JUC forkjoin and completable future
- NRF24L01 troubleshooting
- Naive Bayesian theory derivation
- MySQL replacement field part content
- (4) Data visualization of R language -- matrix chart, histogram, pie chart, scatter chart, linear regression and strip chart
- Unity scene jump and exit
- PRIDE-PPPAR源码解析
- Agile development helps me
猜你喜欢

Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY

Unity3d, Alibaba cloud server, platform configuration

【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现

Fairygui gain buff value change display

Single chip Bluetooth wireless burning

(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis

CUDA C programming authoritative guide Grossman Chapter 4 global memory

JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.

单片机蓝牙无线烧录

SVN更新后不出现红色感叹号
随机推荐
如何给Arduino项目添加音乐播放功能
Vulnhub target: hacknos_ PLAYER V1.1
[leetcode15] sum of three numbers
Compile GDAL source code with nmake (win10, vs2022)
Acwing-116 pilot brother
Teach you to release a DeNO module hand in hand
Office提示您的许可证不是正版弹框解决
JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.
Containers and Devops: container based Devops delivery pipeline
Fairygui gain buff value change display
Office prompts that your license is not genuine pop-up box solution
How to add music playback function to Arduino project
单片机蓝牙无线烧录
Introduction to the daily practice column of the Blue Bridge Cup
Special palindromes of daily practice of Blue Bridge Cup
How to reduce the shutdown time of InnoDB database?
FairyGUI复选框与进度条的组合使用
Database course design: college educational administration management system (including code)
地球围绕太阳转
Common DOS commands