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. LSTM 应用于股票市场

    https://zhuanlan.zhihu.com/p/27112144 1.LSTM对于非平稳数据的预测效果没有平稳数据好 2.神经网络的过拟合:在训练神经网络过程中,“过拟合”是一项尽量要避免的 ...

  2. thinkphp文件上传以及图片处理

    文件上传 上传表单 在ThinkPHP中使用上传功能无需进行特别处理.例如,下面是一个带有附件上传的表单提交: <form action="__URL__/upload" e ...

  3. jQuery 样式操作、文档操作、属性操作的方法总结

    文档操作: addClass()             向匹配的元素添加指定的类名.after()                    在匹配的元素之后插入内容.append()         ...

  4. DS博客作业05—树

    1.本周学习总结 1.1思维导图 1.2学习体会 本周学习了树的相关知识,了解了树结构体的应用和基本操作 学习了二叉树的遍历,创建以及哈夫曼树的相关操作 通过树的构建等操作熟练了递归的使用 2.PTA ...

  5. 【Luogu】P4208最小生成树计数(状压乱搞)

    题目链接 最小生成树有两个性质,两个性质都知道的话这题就变成码农题了. 1.无论最小生成树长什么样,所有权值的边的数量是不变的.比如我有棵最小生成树有两条权值为2的边四条权值为1的边,那这个图的所有最 ...

  6. Code Jam 2017 Round 1A Problem B. Ratatouille

    传送门 分析 首先把包(package)的克数 $Q_{ij}$ 转化成区间 $[\lceil Q_{ij}/(1.1 r_i )\rceil, \lfloor Q_{ij}/(0.9 r_i)\rf ...

  7. 【CTSC2010】产品销售(bzoj1920)

    数据结构优化网络流…… 重新定义一下题目的各种条件: 第 $i$ 天能生产 $a_i$ 个物品: 第 $i$ 天有 $b_i$ 个物品的需求: 每存储一天物品(把订单提前一天)需要 $c_i$ 的花费 ...

  8. C# IEnumerable to List 的转换

    一.使用Linq using System.Linq; Example: IEnumerable<, ); List<int> asList = enumerable.ToList( ...

  9. bzoj 2797 [Poi2012]Squarks 枚举一个,推出所有

    题目大意 设有n个互不相同的正整数{X1,X2,...Xn},任取两个Xi,Xj(i≠j),能算出Xi+Xj. 现在所有取法共n*(n-1)/2个和,要你求出X1,X2,...Xn. 输出所有满足条件 ...

  10. FOJ Problem 2256 迷宫

                                                                                                        ...