Golang | Leetcode Golang题解之第456题132模式

题目:

题解:

Go 复制代码
func find132pattern(nums []int) bool {
    candidateI, candidateJ := []int{-nums[0]}, []int{-nums[0]}

    for _, v := range nums[1:] {
        idxI := sort.SearchInts(candidateI, 1-v)
        idxJ := sort.SearchInts(candidateJ, -v)
        if idxI < idxJ {
            return true
        }

        if v < -candidateI[len(candidateI)-1] {
            candidateI = append(candidateI, -v)
            candidateJ = append(candidateJ, -v)
        } else if v > -candidateJ[len(candidateJ)-1] {
            lastI := -candidateI[len(candidateI)-1]
            for len(candidateJ) > 0 && v > -candidateJ[len(candidateJ)-1] {
                candidateI = candidateI[:len(candidateI)-1]
                candidateJ = candidateJ[:len(candidateJ)-1]
            }
            candidateI = append(candidateI, -lastI)
            candidateJ = append(candidateJ, -v)
        }
    }

    return false
}
相关推荐
圣保罗的大教堂15 小时前
leetcode 3517. 最小回文排列 I 中等
leetcode
名字还没想好☜16 小时前
Go 的 time.After 在 select 循环里内存泄漏:定时器堆积原理与 timer.Reset 正确姿势
java·数据库·golang·go·goroutine
alphaTao17 小时前
LeetCode 每日一题 2026/7/27-2026/8/2
python·算法·leetcode
ttwuai17 小时前
GoFrame 后台日志清空失败:无 WHERE 删除为什么被拦住
前端·golang
进击的程序猿~18 小时前
Go 并发底层原理面试学习指南
开发语言·面试·golang
不在逃避q19 小时前
Golang笔记之Redis
redis·笔记·golang
Hi李耶1 天前
【LeetCode】9-回文数
算法·leetcode·职场和发展
geovindu1 天前
go:loghelper
开发语言·后端·golang
呐抹倾1 天前
go学习笔记:panic是什么含义
笔记·学习·golang
海绵天哥1 天前
LeetCode Hot 100 | 链表(下)· 分组翻转与设计(C++ 题解)
c++·leetcode·链表