https://leetcode.com/problems/132-pattern/

下面是我的做法。后来又看了一个提示:

https://discuss.leetcode.com/topic/67881/single-pass-c-o-n-space-and-time-solution-8-lines-with-detailed-explanation

里面的解法非常好。先从后向前找到中间那个数。利用了栈的特性,非常好。

  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. class Solution {
  6. public boolean find132pattern(int[] nums) {
  7. List<Integer> start = new ArrayList();
  8. List<Integer> end = new ArrayList();
  9.  
  10. int small = Integer.MAX_VALUE;
  11. int big = Integer.MIN_VALUE;
  12.  
  13. for (int i=; i<nums.length; i++) {
  14. if (nums[i] == nums[i-]) {
  15. continue;
  16. }
  17. if (nums[i] > nums[i-]) {
  18. if (nums[i] > big) {
  19. big = nums[i];
  20. }
  21. if (nums[i-] < small) {
  22. small = nums[i-];
  23. }
  24. for (int k=; k<start.size(); k++) {
  25. if (nums[i] > start.get(k) && nums[i] < end.get(k)) {
  26. return true;
  27. }
  28. }
  29. }
  30. else {
  31. if (nums[i] > small) {
  32. return true;
  33. }
  34. for (int k=; k<start.size(); k++) {
  35. if (nums[i] > start.get(k) && nums[i] < end.get(k)) {
  36. return true;
  37. }
  38. }
  39. if (big > Integer.MIN_VALUE) {
  40. start.add(small);
  41. end.add(big);
  42. //System.out.printf("start: %d, end: %d\n", small, big);
  43. small = Integer.MAX_VALUE;
  44. big = Integer.MIN_VALUE;
  45. }
  46.  
  47. }
  48. }
  49. return false;
  50. }
  51. }
  52.  
  53. public class Main {
  54.  
  55. public static void main(String[] args) throws InterruptedException {
  56.  
  57. System.out.println("Hello!");
  58. Solution solution = new Solution();
  59.  
  60. // Your Codec object will be instantiated and called as such:
  61. int[] g = {, , , , };
  62. boolean ret = solution.find132pattern(g);
  63. System.out.printf("ret:%b\n", ret);
  64.  
  65. System.out.println();
  66.  
  67. }
  68.  
  69. }

132-pattern(蛮难的)的更多相关文章

  1. LeetCode解题报告—— 1-bit and 2-bit Characters & 132 Pattern & 3Sum

    1. 1-bit and 2-bit Characters We have two special characters. The first character can be represented ...

  2. 【LeetCode】456. 132 Pattern 解题报告(Python)

    [LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

  3. [LeetCode] 132 Pattern 132模式

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  4. Leetcode: 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...

  5. 456. 132 Pattern

    456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...

  6. 【LeetCode】456. 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  7. LeetCode 456. 132 Pattern

    问题描述 给一组数,判断这组数中是否含有132 pattern. 132 pattern: i < j < k, 且 ai < ak < aj 第一种解法 使用栈来保存候选的子 ...

  8. [Swift]LeetCode456. 132模式 | 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that  ...

  9. circular-array-loop(蛮难的)

    https://leetcode.com/problems/circular-array-loop/ 题目蛮难的,有一些坑. 前后两个指针追赶找环的方法,基本可以归结为一种定式.可以多总结. pack ...

  10. [leetcode] 456. 132 Pattern (Medium)

    对一个三个元素以上的数组,如果存在1-3-2模式的组合,则返回true. 1-3-2模式就是值的排序是i<k<j但是下标排序是i<j<k. 解法一: 硬解,利用一个变量存储是否 ...

随机推荐

  1. PHP中文网 学习阶段规划

    1.第一阶段: 前端基础 前端基础课程大纲 教学内容 教学重点 1.HTML5 HTML简介.HTML标签详解.字符编码的奥秘.Html5新特性与常用标签 2.CSS3 CSS简介.CSS的引入方式. ...

  2. 树链剖分 - Luogu 3384【模板】树链剖分

    [模板]树链剖分 题目描述 已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z 操 ...

  3. 流式处理框架storm浅析(上篇)

    本文来自网易云社区 作者:汪建伟 前言 前一段时间参与哨兵流式监控功能设计,调研了两个可以做流式计算的框架:storm和spark streaming,我负责storm的调研工作.断断续续花了一周的时 ...

  4. 如果奇迹有颜色,那么一定是暴力or模拟比较6

    模拟就是一个思想,给你一个东西,没有很好的算法去解决,只需要计算机去暴力,优雅的暴力就可以叫算法了 主要还是考大家的代码能力,这次题目应该不需要任何前置技能. 1001 Time Limit(Comm ...

  5. poj1006 中国剩余定理&&中国剩余定理解析

    poj 1006 题的思路不是很难的,可以转化数学式: 现设 num 是下一个相同日子距离开始的天数 p,e,i,d 如题中所设! 那么就可以得到三个式子:( num + d ) % 23 == p: ...

  6. Linux Shell系列教程之(八)Shell printf命令详解

    本文是Linux Shell系列教程的第(八)篇,更多shell教程请看:Linux Shell系列教程 在上一篇:Linux Shell系列教程之(七)Shell输出这篇文章中,已经对Shell p ...

  7. Z-Score数据标准化处理(python代码)

    #/usr/bin/python def Z_Score(data): lenth = len(data) total = sum(data) ave = float(total)/lenth tem ...

  8. 背包问题--golang的入门解

    . 定义一种 表示静态属性的 关键字 / 修饰符 2. 作用共用.共享 能有此作用的原因分析: Java中,任何变量 / 代码存储时,都是 在编译时 由系统自动分配内存在静态变量编译后,所分配的内存会 ...

  9. vue.js源码学习分享(三)

    /** * Mix properties into target object.//把多个属性插入目标的对象 */ function extend (to, _from) { for (var key ...

  10. 【開發時,應注意事項】 vendor tools 無法 work 時,怎麼辦?

    遇到 vendor tools 無法 work 時, 最好的方法直接請 vendor 來, 為什麼呢? 因為 tool 可能 有版本的問題, 譬如: vendor tool A tool 在 buil ...