美文网首页
2022-01-12 343 递增的三元子序列

2022-01-12 343 递增的三元子序列

作者: 16孙一凡通工 | 来源:发表于2022-01-17 10:06 被阅读0次

java版本:

func increasingTriplet(nums []int) bool {

    
   
    // a 始终记录最小元素,b 为某个子序列里第二大的数。
    // 接下来不断更新 a,同时保持 b 尽可能的小。
   // 如果下一个元素比 b 大,说明找到了三元组。
    if(len(nums)<3){
        return false;
    }
 
    a,b:=math.MaxInt64,math.MaxInt64;
    for i:=0;i<len(nums);i++{
      if(nums[i]<=a){
          a=nums[i];
      }else if(nums[i]<=b){
          b=nums[i];
      }else{
          return true;
      }
    
    }
    return false;
}

相关文章

网友评论

      本文标题:2022-01-12 343 递增的三元子序列

      本文链接:https://www.haomeiwen.com/subject/qucncrtx.html