public class Solution {
public boolean increasingTriplet(int[] nums) {
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
for (int n : nums) {
// 发现更小的数,更新当前最小,以便一会更新当前第二小
if (n <= first) first = n;
// 这是当前第二小的数,即使first已经被更新过,但不影响第二小,因为
// 当前第二小之前一定有比它更小的一个数
else if (n <= second) second = n;
// 说明当前数比第二小的大,则说明有三元递增序列
else return true;
}
return false;
}
} class Solution:
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
min1 = float('inf')
min2 = float('inf')
for n in nums:
if n <= min1:
min1 = n
elif n <= min2:
min2 = n
else:
return True
return False