LeetCode OJ 456 132 Pattern [Medium]
来源:互联网 发布:七月算法 视频 编辑:程序博客网 时间:2024/06/06 02:58
题目描述:
Given a sequence ofn integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such thati < j < k and ai < ak < aj. Design an algorithm that takes a listof n numbers as input and checks whether there is a 132 pattern in the list.
Note:n will be less than 15,000.
Example 1:
Input: [1, 2, 3, 4]
Output: False
Explanation: Thereis no 132 pattern in the sequence.
Example 2:
Input: [3, 1, 4, 2]
Output: True
Explanation: Thereis a 132 pattern in the sequence: [1, 4, 2].
Example 3:
Input: [-1, 3, 2, 0]
Output: True
Explanation: Thereare three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].
题目理解 :
给定一个有n个数的整数序列,判断是否为132序列
132序列定义:这个序列中存在 小的在大的前面,大的后面有一个数的大小介于中间的这样一个序列。
我的分析:
1. 对一个序列,找一个smaller,找一个bigger,smaller在bigger之前,在bigger之后有一个介于它们中间的数
2. 如图分析:在序列smaller,bigger之后存在一个大小介于它们之间的数字
3. 无论当升序(当前值比前面的大)还是降序(当前值比前面的小),都首先考虑是否符合132 patter,若是,则返回true。
4. 若不符合132 patter:
(1) 若是升序,则 更改/不改 当前上升线的最大值bigger
(2) 若是降序,则开始新的上升线,填写新的smaller
5. 用两个数组smaller,bigger来存储每条上升线的最小值和最大值,索引相同表示是一条上升线,如smaller[2]和bigger[2]表示一条上升线的最小值和最大值
我的解答:
static public boolean find132pattern(int[] nums) { if(nums.length == 0) return false; int[] smaller = new int[15000]; int[] bigger = new int[15000]; for (int i = 0; i < smaller.length; i++) { smaller[i] = Integer.MAX_VALUE; bigger[i] = Integer.MIN_VALUE; } smaller[0] = nums[0]; for (int i = 0, j = 0; i < nums.length - 1; i++) { for (int n = 0; n <= j; n++) { if (nums[i + 1] > smaller[n] && nums[i + 1] < bigger[n]) { return true; } } if (nums[i] < nums[i + 1]) { if (nums[i + 1] > bigger[j]) { bigger[j] = nums[i + 1]; } } else if (nums[i] > nums[i + 1]) { j += 1; smaller[j] = nums[i+1]; } } return false;}
其他解答:
I use the variables begand end to keep track of minimum subarray A[beg...end] which must be sorted forthe entire array A to be sorted. If end < beg< 0 at the end of the for loop, then thearray is already fully sorted.
public int findUnsortedSubarray(int[] A) { int n = A.length, beg = -1, end = -2, min = A[n-1], max = A[0]; for (int i=1;i<n;i++) { max = Math.max(max, A[i]); min = Math.min(min, A[n-1-i]); if (A[i] < max) end = i; if (A[n-1-i] > min) beg = n-1-i; } return end - beg + 1;}
- LeetCode OJ 456 132 Pattern [Medium]
- Leetcode OJ 46. Permutations [Medium]
- Leetcode OJ 78 Subsets [medium]
- Leetcode OJ 77 Combinations [Medium]
- LeetCode OJ 279 Perfect Squares [Medium]
- LeetCode OJ 306 Additive Number [Medium]
- LeetCode OJ 15 3Sum [Medium]
- LeetCode OJ 56 Merge Intervals [Medium]
- Leetcode OJ 73 Set Matrix Zeroes [Medium]
- Leetcode OJ 127 Word Ladder [Medium]
- Leetcode OJ 2 Add Two Numbers [Medium]
- Leetcode OJ 12 Integer to Roman [Medium]
- Leetcode OJ 22 Generate Parentheses [Medium]
- Leetcode OJ 22 Generate Parentheses [Medium]
- Leetcode OJ 49. Group Anagrams [Medium]
- Leetcode OJ 79 Word Search [Medium]
- Leetcode OJ 91 Decode Ways [Medium]
- Leetcode OJ 131 Palindrome Partitioning [Medium]
- 实现二进制与十进制之间的相互转换
- Thinking in java-2 Java内存模型
- Unity3D基础篇----Shader学习笔记(3)
- Matlab figure传入数据到figure(二)
- 树的遍历
- LeetCode OJ 456 132 Pattern [Medium]
- Jni数据类型
- Java用数组实现顺序队列
- 主题三 编译过程介绍----17.条件编译的使用分析
- A20地址线问题
- 矩阵的转置
- BMS电池管理系统被动均衡现象描述
- 【已解决】Linux redhat 6.4上安装VNC Server
- Html-CSS