public class Solution { public bool IncreasingTriplet(int[] nums) { var len = nums.Length; if (len < 3) { return false; } for (int i = 0; i <= len - 3; i++) { for (int j = i + 1; j <= len - 2; j++) { if (nums[i] < nums[j]) { for (int k = j + 1; k <= len - 1; k++) { if (nums[j] < nums[k]) { return true; } } } } } return false; } }
补充一个python实现:
1 import sys 2 class Solution: 3 def increasingTriplet(self, nums: 'List[int]') -> bool: 4 smaller = sys.maxsize 5 small = sys.maxsize 6 for num in nums: 7 if num <= smaller: 8 smaller = num 9 elif num <= small: 10 small = num 11 else: 12 return True 13 return False