LeetCode 334 Increasing Triplet Subsequence
2016-07-03 18:06
489 查看
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。
设置一个small,一个big,small<big。如果能找到一个大于small和big的就返回true。时间复杂度为O(n)。
public boolean increasingTriplet(int[] nums) {
int small = Integer.MAX_VALUE, big = Integer.MAX_VALUE;
for (int value : nums) {
if (value <= small) small = value;
else if (value <= big) big = value;
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。
设置一个small,一个big,small<big。如果能找到一个大于small和big的就返回true。时间复杂度为O(n)。
public boolean increasingTriplet(int[] nums) {
int small = Integer.MAX_VALUE, big = Integer.MAX_VALUE;
for (int value : nums) {
if (value <= small) small = value;
else if (value <= big) big = value;
else return true;
}
return false;
}
相关文章推荐
- vue-cli需要的包
- EasyUI学习总结(二)——EasyUI布局
- EasyUI学习总结(一)——EasyUI入门
- cQueue类型定义
- iOS GPUImage之GPUImageMovie视频滤镜(2)
- 代码获取storyboard中定义的UIViewController
- EXP-00091: Exporting questionable statistics.
- UILabel 的文字顶部对齐
- UIButton
- 我的音乐播放器(1)ui界面
- UIView
- Android组件及UI框架大全
- vuejs动态组件给子组件传递数据
- UIActivityIndicatorView
- a build only device cannot be used to run this targe
- [leetcode-357]Count Numbers with Unique Digits(java)
- 2015最流行的Android组件、工具、框架大全 UI框架大全
- UITextField属性(二)
- JAVA线程池ThreadPoolExecutor与阻塞队列BlockingQueue
- make: *** /lib/modules/2.6.32-573.el6.x86_64/build: 没有那个文件或目录。 停止。