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. python - work4

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: work_20181108.py@ide: PyCharm Communit ...

  2. python基础补漏-08-异常处理

    try: #正常代码逻辑 ins = raw_input("this is a tast:") print ins+1 except Exception,e: print e -- ...

  3. Install Oracle 11G Release 2 (11.2) on Centos Linux 7

    Install Oracle 11G Release 2 (11.2) on Centos Linux 7 This article presents how to install Oracle 11 ...

  4. PHP 修改配置文件后重启命名

    centosPHP配置文件路径: /etc/php.ini 修改完配置文件后需要重启php服务: systemctl restart php-fpm

  5. pip安装及使用

    1.pip下载安装 1.1 pip下载 # wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5= ...

  6. Learning Deconvolution Network for Semantic Segme小结

    题目:Learning Deconvolution Network for Semantic Segmentation 作者:Hyeonwoo Noh, Seunghoon Hong, Bohyung ...

  7. 周赛Problem 1025: Hkhv love spent money(RMQ)

    Problem 1025: Hkhv love spent money Time Limits:  1000 MS   Memory Limits:  65536 KB 64-bit interger ...

  8. [CODEVS1911] 孤岛营救问题(分层图最短路)

    传送门 吐槽:神tm网络流... 用持有的钥匙分层,状态压缩,用 2 进制表示持有的钥匙集合. dis[i][j][k] 表示持有的钥匙集合为 k,到达点 (i, j) 的最短路径. 分层图的最短路听 ...

  9. [luoguP2526] [SHOI2001]小狗散步(二分图最大匹配)

    传送门 简直就是模板题啊! #include <cmath> #include <cstdio> #include <cstring> #include <i ...

  10. BZOJ4916 神犇和蒟蒻 【欧拉函数 + 杜教筛】

    题目 很久很久以前,有一只神犇叫yzy; 很久很久之后,有一只蒟蒻叫lty; 输入格式 请你读入一个整数N;1<=N<=1E9,A.B模1E9+7; 输出格式 请你输出一个整数A=\sum ...