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
}
相关推荐
Norvyn_71 小时前
LeetCode|Day18|20. 有效的括号|Python刷题笔记
笔记·python·leetcode
呆呆的小鳄鱼2 小时前
leetcode:冗余连接 II[并查集检查环][节点入度]
算法·leetcode·职场和发展
墨染点香2 小时前
LeetCode Hot100【6. Z 字形变换】
java·算法·leetcode
沧澜sincerely2 小时前
排序【各种题型+对应LeetCode习题练习】
算法·leetcode·排序算法
CQ_07122 小时前
自学力扣:最长连续序列
数据结构·算法·leetcode
YuTaoShao2 小时前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
好易学·数据结构11 小时前
可视化图解算法56:岛屿数量
数据结构·算法·leetcode·力扣·回溯·牛客网
墨染点香13 小时前
LeetCode Hot100【5. 最长回文子串】
算法·leetcode·职场和发展
ん贤13 小时前
如何加快golang编译速度
后端·golang·go
riverz122715 小时前
Go 程序无法使用 /etc/resolv.conf 的 DNS 配置排查记录
golang