Increasing Triplet Subsequence
2016-02-22 20:48
525 查看
334.Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
Formally the function should:
Return true if there exists i, j, k
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
Your algorithm should run in O(n) time complexity and O(1) space complexity.
Examples:
Given
return
Given
return
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
Formally the function should:
Return true if there exists i, j, k
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
Your algorithm should run in O(n) time complexity and O(1) space complexity.
Examples:
Given
[1, 2, 3, 4, 5],
return
true.
Given
[5, 4, 3, 2, 1],
return
false.
class Solution { public: bool increasingTr a0d1 iplet(vector<int>& nums) { int first = INT_MAX; int second = INT_MAX; //通过两个变量记录序列的前两个值 for (size_t i=0;i+1<nums.size();++i) { if (nums[i]<nums[i+1]&&first>nums[i]&&second>nums[i+1]) //当first与second均比当前值和随后值大时更新 { first = nums[i]; second = nums[i+1]; } if (nums[i]>second||nums[i+1]>second) //若当前值或随后值比second大则形成递增序列 { return true; } if (nums[i]>first&&nums[i]<second) //若当前值处于first和second中间,更新second { second = nums[i]; } } return false; } };
相关文章推荐
- UISegmentControl 、UIStepper
- Android Build (1) -- Source Code Build Steps
- 一个关于 UIPickerView 的 bug
- 1099. Build A Binary Search Tree (30)
- 被玩坏的innerHTML、innerText、textContent和value属性
- HDU1711-Number Sequence-KMP
- 1.UILabel
- iOS蓝牙开发(四):BabyBluetooth蓝牙库介绍
- ZOJ 3210 A Stack or A Queue?(暴力)
- StringBuilder StringBuffer区别
- CCSprite精灵类创建、坐标系OpenGL、坐标系UIKit坐标系(屏幕坐标系)-----
- LeetCode-Range Sum Query - Immutable
- 选中输入框上弹视图
- 基于UIKit下的UIView动画
- String、StringBuilder、StringBuffer
- UIVisualEffectView(自定义模糊视图)
- SSH+Easyui之combotree ,TreeGrid 树形展现数据
- SourceTree——MAC OSX下的Git GUI客户端
- UINavigationController + UIScrollView组合,视图尺寸的设置探秘(三)
- UINavigationController + UIScrollView组合,视图尺寸的设置探秘(二)