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

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

package com.company;

import java.util.*;

class Solution {
public boolean find132pattern(int[] nums) {
List<Integer> start = new ArrayList();
List<Integer> end = new ArrayList(); int small = Integer.MAX_VALUE;
int big = Integer.MIN_VALUE; for (int i=; i<nums.length; i++) {
if (nums[i] == nums[i-]) {
continue;
}
if (nums[i] > nums[i-]) {
if (nums[i] > big) {
big = nums[i];
}
if (nums[i-] < small) {
small = nums[i-];
}
for (int k=; k<start.size(); k++) {
if (nums[i] > start.get(k) && nums[i] < end.get(k)) {
return true;
}
}
}
else {
if (nums[i] > small) {
return true;
}
for (int k=; k<start.size(); k++) {
if (nums[i] > start.get(k) && nums[i] < end.get(k)) {
return true;
}
}
if (big > Integer.MIN_VALUE) {
start.add(small);
end.add(big);
//System.out.printf("start: %d, end: %d\n", small, big);
small = Integer.MAX_VALUE;
big = Integer.MIN_VALUE;
} }
}
return false;
}
} public class Main { public static void main(String[] args) throws InterruptedException { System.out.println("Hello!");
Solution solution = new Solution(); // Your Codec object will be instantiated and called as such:
int[] g = {, , , , };
boolean ret = solution.find132pattern(g);
System.out.printf("ret:%b\n", ret); System.out.println(); } }

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. 26、android上跑apache的ftp服务

    一.为啥 在android设备跑ftp服务,在现场方便查看日志,目前就是这么用的. 二.前提: 从apache的官网下载依赖包:http://mina.apache.org/ftpserver-pro ...

  2. 设计模式之单例模式 Singleton

    核心作用 保证一个类只有一个实例,并且提供一个访问该实例的全局访问点. 常见应用场景 优点 由于单例模式只生成一个实例,减少了系统性开销,当一个对象的产生需要比较多的资源时,如读取配置,产生其他依赖对 ...

  3. 爬虫Scrapy框架-Crawlspider链接提取器与规则解析器

    Crawlspider 一:Crawlspider简介 CrawlSpider其实是Spider的一个子类,除了继承到Spider的特性和功能外,还派生除了其自己独有的更加强大的特性和功能.其中最显著 ...

  4. [python IO学习篇] [打开包含中文路径的文件]

    https://my.oschina.net/mcyang000/blog/289460   打开路径含有中文的文件时,要注意: 1 在windows对文件名编码是采用gbk等进行编码保存,所以要将文 ...

  5. 阿里云 fatal: Authentication failed 退出码 128

    阿里云拉取代码时需要输入的用户名密码 跟我们登录aliyun的时候的账号密码不一样,需要先重置一下的,重置之前要配置好邮箱 https://code.aliyun.com/profile/passwo ...

  6. 项目记事【多线程】:关于 SimpledDateFormat 的多线程问题

    背景: 最近项目引入了 SonarLink,解决代码规范的问题,在检查历史代码的时候,发现了一个问题. 先看代码: public class DateUtil { private static fin ...

  7. 刷题总结——字符串(ssoj)

    题目: 给定n个小的字符串T和一个大的字符串S,先输出T总共再S中出现了多少次 然后q个询问···每次修改S上的一个字母,然后再次输出上述答案··· n小于1000,q<200000,T的总长度 ...

  8. 如何查找Windows上安装的DB2的端口号Port

    1.db2com打开控制台 2.db2 get dbm cfg 可以通过db2 get dbm cfg,查询数据库管理器配置参数,就可以查到端口号或端口名. 示例: $ db2 get dbm cfg ...

  9. java面试题之synchronized和lock有什么区别

    synchronized和lock的区别: 类别 synchronized lock 存在层次 java的关键字,在jvm层面上 是一个类 锁的释放 1.以获取锁的线程执行完同步代码,释放锁 2.线程 ...

  10. cf496D Tennis Game

    Petya and Gena love playing table tennis. A single match is played according to the following rules: ...