我正在解决以下问题:
Given an unsorted array return whether an increasing subsequence of length 3 exists in the array or not. Formally return true if there exists i, j, k such that:
arr[i]<arr[j]<arr[k] given 0 <= i < j < k <= n-1
这是我的代码:
public boolean increasingTriplet(int[] nums) {
if (nums.length < 3) return false;
for (int i = 0; i < nums.length-2; i++) {
if (nums[i] < nums[i+1]) {
if (nums[i+1] < nums[i+2]) return true;
}
}
return false;
}
我的代码在以下输入中失败:
[5,1,5,5,2,5,4] 显然,对于此序列,我的代码应该返回true,但是由于我看不到长度3的任何增加的子序列,所以我无法弄清楚为什么这样做,对于理解此问题的任何帮助,我们将不胜感激。
芜湖不芜
侃侃尔雅
相关分类