2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_334.java 1001 Bytes
一键复制 编辑 原始数据 按行查看 历史
CG国斌 提交于 2020-11-01 10:02 . 中等难度:字符串 & 链表
package com.hit.basmath.interview.top_interview_questions.medium_collection.array_and_strings;
/**
* 334. 递增的三元子序列
* <p>
* 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。
* <p>
* 数学表达式如下:
* 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,
* 使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。
* 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) 。
* <p>
* 示例 1:
* <p>
* 输入: [1,2,3,4,5]
* <p>
* 输出: true
* 示例 2:
* <p>
* 输入: [5,4,3,2,1]
* 输出: false
*/
public class _334 {
public boolean increasingTriplet(int[] nums) {
int min = Integer.MAX_VALUE, secondMin = Integer.MAX_VALUE;
for (int num : nums) {
if (num <= min) min = num;
else if (num < secondMin) secondMin = num;
else if (num > secondMin) return true;
}
return false;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助