132-pattern(蛮难的)
https://leetcode.com/problems/132-pattern/
下面是我的做法。后来又看了一个提示:
里面的解法非常好。先从后向前找到中间那个数。利用了栈的特性,非常好。
- 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(蛮难的)的更多相关文章
- 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 ...
- 【LeetCode】456. 132 Pattern 解题报告(Python)
[LeetCode]456. 132 Pattern 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- [LeetCode] 132 Pattern 132模式
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- Leetcode: 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- 456. 132 Pattern
456. 132 Pattern Given an array of integers a1, a2, a3-an, judge if there exists the 132 pattern. 13 ...
- 【LeetCode】456. 132 Pattern
Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that ...
- LeetCode 456. 132 Pattern
问题描述 给一组数,判断这组数中是否含有132 pattern. 132 pattern: i < j < k, 且 ai < ak < aj 第一种解法 使用栈来保存候选的子 ...
- [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 ...
- circular-array-loop(蛮难的)
https://leetcode.com/problems/circular-array-loop/ 题目蛮难的,有一些坑. 前后两个指针追赶找环的方法,基本可以归结为一种定式.可以多总结. pack ...
- [leetcode] 456. 132 Pattern (Medium)
对一个三个元素以上的数组,如果存在1-3-2模式的组合,则返回true. 1-3-2模式就是值的排序是i<k<j但是下标排序是i<j<k. 解法一: 硬解,利用一个变量存储是否 ...
随机推荐
- PHP中文网 学习阶段规划
1.第一阶段: 前端基础 前端基础课程大纲 教学内容 教学重点 1.HTML5 HTML简介.HTML标签详解.字符编码的奥秘.Html5新特性与常用标签 2.CSS3 CSS简介.CSS的引入方式. ...
- 树链剖分 - Luogu 3384【模板】树链剖分
[模板]树链剖分 题目描述 已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节点的值都加上z 操 ...
- 流式处理框架storm浅析(上篇)
本文来自网易云社区 作者:汪建伟 前言 前一段时间参与哨兵流式监控功能设计,调研了两个可以做流式计算的框架:storm和spark streaming,我负责storm的调研工作.断断续续花了一周的时 ...
- 如果奇迹有颜色,那么一定是暴力or模拟比较6
模拟就是一个思想,给你一个东西,没有很好的算法去解决,只需要计算机去暴力,优雅的暴力就可以叫算法了 主要还是考大家的代码能力,这次题目应该不需要任何前置技能. 1001 Time Limit(Comm ...
- poj1006 中国剩余定理&&中国剩余定理解析
poj 1006 题的思路不是很难的,可以转化数学式: 现设 num 是下一个相同日子距离开始的天数 p,e,i,d 如题中所设! 那么就可以得到三个式子:( num + d ) % 23 == p: ...
- Linux Shell系列教程之(八)Shell printf命令详解
本文是Linux Shell系列教程的第(八)篇,更多shell教程请看:Linux Shell系列教程 在上一篇:Linux Shell系列教程之(七)Shell输出这篇文章中,已经对Shell p ...
- Z-Score数据标准化处理(python代码)
#/usr/bin/python def Z_Score(data): lenth = len(data) total = sum(data) ave = float(total)/lenth tem ...
- 背包问题--golang的入门解
. 定义一种 表示静态属性的 关键字 / 修饰符 2. 作用共用.共享 能有此作用的原因分析: Java中,任何变量 / 代码存储时,都是 在编译时 由系统自动分配内存在静态变量编译后,所分配的内存会 ...
- vue.js源码学习分享(三)
/** * Mix properties into target object.//把多个属性插入目标的对象 */ function extend (to, _from) { for (var key ...
- 【開發時,應注意事項】 vendor tools 無法 work 時,怎麼辦?
遇到 vendor tools 無法 work 時, 最好的方法直接請 vendor 來, 為什麼呢? 因為 tool 可能 有版本的問題, 譬如: vendor tool A tool 在 buil ...