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. luogu2293 [JSOI2008]Blue Mary开公司

    ref好像叫什么李超线段树?--这篇不是太通用-- #include <iostream> #include <cstdio> #include <cmath> u ...

  2. [Oracle] Lob介绍

    [Oracle] Lob介绍   像Oracle这种关系型数据库,比较擅长处理结构化的数据,那么对于非结构化的数据,Oracle是怎么处理和存储的呢?Lob (Large Object)是Oracle ...

  3. 使用sami生成文档

    从composer安装sami $ composer require sami/sami composer自动配置完以后,可以先测试一下是否安装成功.只要不带参数的运行一下sami,就会知道结果. $ ...

  4. 【bzoj1604】[Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 旋转坐标系+并查集+Treap/STL-set

    题目描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l≤Xi,Yi≤ ...

  5. hdu 1087 最大上升子序列的和(dp或线段树)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. hdu 6119 小小粉丝度度熊

    小小粉丝度度熊 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  7. poj 1981 Circle and Points

    Circle and Points Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 8131   Accepted: 2899 ...

  8. 【BZOJ3529】【SDOI2014】数表 (莫比乌斯反演+树状数组)

    传送门 Description 有一张$n\times m$的数表,其第$i$行第$j$列 $(1≤i≤n,1≤j≤m)$ 的数值为能同时整除$i$和$j$的所有自然数之和.现在给定$a$,计算数表中 ...

  9. LeetCode OJ--Best Time to Buy and Sell Stock III

    http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...

  10. LeetCode OJ——Convert Sorted Array to Binary Search Tree

    http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 将一个升序的数组转换成 height balan ...