05 August 2008
给定一个整数数组 nums,如果存在三个索引 (i, j, k) 使得 i < j < k 且 nums[i] < nums[j] < nums[k],则返回 true。如果不存在这样的索引,则返回 false。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Example 1:
Input: nums = [1,2,3,4,5]
Output: true
Explanation: Any triplet where i < j < k is valid.
Example 2:
Input: nums = [5,4,3,2,1]
Output: false
Explanation: No triplet exists.
Example 3:
Input: nums = [2,1,5,0,4,6]
Output: true
Explanation: The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public boolean increasingTriplet(int[] nums) {
int first=Integer.MAX_VALUE, second=Integer.MAX_VALUE;
for(int n : nums){
if(n <= first) //found first
first = n;
else if(n <= second)//found second bigger than first
second = n;
else //found third , bigger than first and second
return true;
}
return false; //did not find triplet
}
}