334. Increasing Triplet Subsequence
2016-03-03 08:57
381 查看
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
本题也就是一个最长递增子串的问题延伸,求解思路就是不能追求某个值,而是应该利用起数组下标的优势遍历数组,找的是大小关系,而不是某个特定的结果,否则就容易陷入误区。
而关于最长递增子串问题具体可以看joylnwang大神的分析
http://blog.csdn.net/joylnwang/article/details/6766317
public class Solution {
public boolean increasingTriplet(int[] nums) {
int x = nums.length;
if(x < 3) return false;
int a = Integer.MAX_VALUE;
int b = Integer.MAX_VALUE;
for(int i : nums)
{
if(a >= i)a = i;
else if( b >= i) b = 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.
本题也就是一个最长递增子串的问题延伸,求解思路就是不能追求某个值,而是应该利用起数组下标的优势遍历数组,找的是大小关系,而不是某个特定的结果,否则就容易陷入误区。
而关于最长递增子串问题具体可以看joylnwang大神的分析
http://blog.csdn.net/joylnwang/article/details/6766317
public class Solution {
public boolean increasingTriplet(int[] nums) {
int x = nums.length;
if(x < 3) return false;
int a = Integer.MAX_VALUE;
int b = Integer.MAX_VALUE;
for(int i : nums)
{
if(a >= i)a = i;
else if( b >= i) b = i ;
else return true;
}
return false;
}
}
相关文章推荐
- NSOperationQueue与GCD的使用原则和场景
- question_011-JAVA之Collection
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
- HTML中让表单input等文本框为只读不可编辑但可以获取value值的方法;让文本域前面的内容显示在左上角,居中
- Android UI学习 - FrameLayou和布局优化
- 详解 CALayer 和 UIView 的区别和联系
- 数论+差分 hdu1121 Complete the Sequence
- apue file i/o 学习笔记
- 微信团队讲课笔记 Android 开发(二)UI设计
- UINavigationBar UINavigationItem UIBarButtonItem
- 数字及字母混合的验证码
- easyui tabs中href和content加载
- github-MJRefresh(UI)
- SQLite数据中修改某列,对已存在的某列添加UNIQUE约束
- 自定义一个带下划线的UILable
- StringBuilder 与 StringBuffer 源码分析
- 【CodeForces】612C - Replace To Make Regular Bracket Sequence(栈,括号配对问题)
- UIView与CALayer的区别,很详细
- iOS_SN_UITableView的优化
- mvn deploy 报错:Return code is: 400, ReasonPhrase: Bad Request. ->