当前位置: 首页 > news >正文

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

题目:

题解:

class Solution {public boolean find132pattern(int[] nums) {int n = nums.length;List<Integer> candidateI = new ArrayList<Integer>();candidateI.add(nums[0]);List<Integer> candidateJ = new ArrayList<Integer>();candidateJ.add(nums[0]);for (int k = 1; k < n; ++k) {int idxI = binarySearchFirst(candidateI, nums[k]);int idxJ = binarySearchLast(candidateJ, nums[k]);if (idxI >= 0 && idxJ >= 0) {if (idxI <= idxJ) {return true;}}if (nums[k] < candidateI.get(candidateI.size() - 1)) {candidateI.add(nums[k]);candidateJ.add(nums[k]);} else if (nums[k] > candidateJ.get(candidateJ.size() - 1)) {int lastI = candidateI.get(candidateI.size() - 1);while (!candidateJ.isEmpty() && nums[k] > candidateJ.get(candidateJ.size() - 1)) {candidateI.remove(candidateI.size() - 1);candidateJ.remove(candidateJ.size() - 1);}candidateI.add(lastI);candidateJ.add(nums[k]);}}return false;}public int binarySearchFirst(List<Integer> candidate, int target) {int low = 0, high = candidate.size() - 1;if (candidate.get(high) >= target) {return -1;}while (low < high) {int mid = (high - low) / 2 + low;int num = candidate.get(mid);if (num >= target) {low = mid + 1;} else {high = mid;}}return low;}public int binarySearchLast(List<Integer> candidate, int target) {int low = 0, high = candidate.size() - 1;if (candidate.get(low) <= target) {return -1;}while (low < high) {int mid = (high - low + 1) / 2 + low;int num = candidate.get(mid);if (num <= target) {high = mid - 1;} else {low = mid;}}return low;}
}

http://www.mrgr.cn/news/42922.html

相关文章:

  • C语言高阶【2】--动态内存管理【2】--柔性数组(这是个全新的知识点,不想了解一下吗?)
  • C++入门基础知识99——【关于C++ 成员运算符】
  • Pikachu-Unsafe Fileupload-服务端check
  • etcd 快速入门
  • 【有啥问啥】SE(Squeeze-and-Excitation)架构详解
  • CUDA与TensorRT学习六:模型部署-CNN、模型部署-YOLOv8检测器、部署BEVFusion模型
  • vue-scrollto实现页面组件锚点定位
  • 最新版的dubbo服务调用(用nacos做注册中心用)
  • 【pytorch】张量求导
  • 华为杯”第十二届中国研究生数学建模竞赛-D题:单/多列车优化决策问题的研究(续)
  • 第一百零五周周报
  • 帝国CMS迁移网站后出现Create path fail: (建立目录不成功!请检查目录权限 )的解决办法
  • 中断-NVIC与EXTI外设详解(超全面)
  • 用 Delphi 做了一个简单的 CMS
  • 深度学习:基于MindSpore实现CycleGAN壁画修复
  • 认知杂谈98《抵御噪声干扰》
  • Robot Operating System——占据栅格地图(occupancy grid map)
  • valgrind 单例模式的自动释放(多线程)
  • 《计算机原理与系统结构》学习系列——计算机的算数运算(下)
  • SynchronousQueue 的 常用场景及使用示例