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
}
相关推荐
WaitWaitWait014 小时前
LeetCode每日一题4.20
算法·leetcode
蒟蒻小袁4 小时前
力扣面试150题--有效的括号和简化路径
算法·leetcode·面试
Xiaoyu Wang6 小时前
Go协程的调用与原理
开发语言·后端·golang
阳洞洞7 小时前
leetcode 二分查找应用
算法·leetcode·二分查找
Gerry_Liang8 小时前
LeetCode热题100——283. 移动零
数据结构·算法·leetcode
Espresso Macchiato10 小时前
Leetcode 3523. Make Array Non-decreasing
leetcode··leetcode medium·leetcode 3523·leetcode周赛446
Aqua Cheng.12 小时前
25.4.22华为--算法真题整理(2025年4月22日)
java·算法·leetcode·华为·面试
良木林12 小时前
240423 leetcode exercises
算法·leetcode
满怀101512 小时前
【LeetCode】7.整数反转
leetcode·题解
techdashen14 小时前
性能比拼: Go vs Java
java·开发语言·golang