334. Increasing Triplet Subsequence
2016-07-20 15:36
543 查看
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
遍历更新最小的数和次小的数,如果某个数大于这两个数,则有三个数递增,返回true.
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
if (nums.size() < 3) return false;
int firstMin = INT_MAX;
int secondMin = INT_MAX;
for (int i = 0; i < nums.size(); i++){
if (nums[i] <= firstMin){//<=
firstMin = nums[i];
}
else if (nums[i] <= secondMin){
secondMin = nums[i];
}
else{
return true;
}
}
return false;
}
};
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.
遍历更新最小的数和次小的数,如果某个数大于这两个数,则有三个数递增,返回true.
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
if (nums.size() < 3) return false;
int firstMin = INT_MAX;
int secondMin = INT_MAX;
for (int i = 0; i < nums.size(); i++){
if (nums[i] <= firstMin){//<=
firstMin = nums[i];
}
else if (nums[i] <= secondMin){
secondMin = nums[i];
}
else{
return true;
}
}
return false;
}
};
相关文章推荐
- UIStackView 的使用
- String、StringBuffer、StringBuilder的区别
- EF CreateQuery()的命名空间
- easyUI 获取记录数 页数 当前页
- 去掉UICollectionView 在 reloadItems时默认附加的隐式fade动画。
- Unique Paths
- Windows下设备GUID常识
- LeetCode Unique Binary Search Trees动态规划方法详解
- 安装Flash Builder4.6遇到的问题
- iOS开发UI篇-自定义CALayer
- UI阅读笔记
- iOS UILabel设置行间距
- iOS开发从入门到精通-- UIProgressView进度条&UISlider滑动条
- 如何做好UI设计
- UITableView中cell上按钮获取所选cell
- EasyUI之Layout布局
- EasyUI之Layout布局
- EasyUI之Layout布局
- iOS巅峰之点击UIAlertView的灰色部分也能dismiss消失
- request.getQueryString