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
}
相关推荐
We་ct7 小时前
LeetCode 148. 排序链表:归并排序详解
前端·数据结构·算法·leetcode·链表·typescript·排序算法
x_xbx9 小时前
LeetCode:2. 两数相加
算法·leetcode·职场和发展
_日拱一卒9 小时前
LeetCode:最长连续序列
算法·leetcode·职场和发展
重生之后端学习10 小时前
287. 寻找重复数
数据结构·算法·leetcode·深度优先·图论
fy1216311 小时前
GO 快速升级Go版本
开发语言·redis·golang
实心儿儿11 小时前
算法7:两个数组的交集
算法·leetcode·职场和发展
sheeta199812 小时前
LeetCode 每日一题笔记 日期:2025.03.19 题目:3212.统计X和Y频数相等的子矩阵数量
笔记·leetcode·矩阵
Storynone12 小时前
【Day28】LeetCode:509. 斐波那契数,70. 爬楼梯,746. 使用最小花费爬楼梯
python·算法·leetcode
博风13 小时前
算法:双指针解:盛最多水的容器
算法·leetcode
童话ing13 小时前
【Golang】Golang Map数据结构底层原理
数据结构·golang·哈希算法