标签:func 变量 for 遍历 test let ++ turn seq
设两个变量firstSmall和secondSmall,初始化都设置成Infinity。这样在遍历数组的时候可以找到第一小和第二小的数字。当遇到第三个数字小于Infinity但是大于前两者时,就找到了triplet。
注意所谓的subsequence可以是不连续的,如下testcase一样可以通过。[1,3,2,4,0]
1 /** 2 * @param {number[]} nums 3 * @return {boolean} 4 */ 5 var increasingTriplet = function(nums) { 6 let first = Infinity; 7 let second = Infinity; 8 for (let i = 0; i < nums.length; i++) { 9 let cur = nums[i]; 10 if (cur <= first) { 11 first = cur; 12 } else if (cur <= second) { 13 second = cur; 14 } else { 15 return true; 16 } 17 } 18 return false; 19 };
[LeetCode] 334. Increasing Triplet Subsequence
标签:func 变量 for 遍历 test let ++ turn seq
原文地址:https://www.cnblogs.com/aaronliu1991/p/11639306.html