leetcode_334 Increasing Triplet Subsequence

原创 2016年06月01日 23:01:41
  • 题目分析:

    给定一个长度为n的乱序数组nums,求数组中是否存在i,j,k,使得num[i] < num[j] < num[k](0 <= i < j < k <= n-1)。

  • 解题思路:

    设x1为遍历到当前时的最小值,x2为遍历当前时最少有一个数比x2小的数。

    初始时,将x1与x2均设置INT_MAX,然后遍历数组,不断更新x1和x2,其更原则为:

    1)若nums[i] <= x1,则x1 = nums[i];

    2)若x1 < num[i] <= x2,则x2 = nums[i]

    3)nums[i] < x2,说明有解,返回true即可。

    实质:不断缩小x1和x2,看是否存在第3个大于x2,存在,则为答案。

  • 实现程序

    • C++版本

      class Solution
      {
          public:
              bool increasingTriplet(vector<int> &nums)
              {
                  int x1 = 0x7fffffff;
                  int x2 = 0x7fffffff;
                  for (int i = 0; i < nums.size(); i++)
                  {
                      if (nums[i] <= x1)
                          x1 = nums[i];
                      else if (nums[i] <= x2)
                          x2 = nums[i];
                      else 
                          return true;
                  }
                  return false;
              }
      };
      
    • Java版本

      public boolean increasingTriplet(int[] nums){
          int x1 = 0x7fffffff;
          int x2 = 0x7fffffff;
          for (int i = 0; i < nums.length; i++)
          {   
              if (nums[i] <= x1){
                  // 记录遍历到当前时,出现的最小元素
                  x1 = nums[i];
              } else if (nums[i] <= x2) {
                  // 记录遍历到当前,出现的第二小的元素
                  x2 = nums[i];
              }
              else {
                  return true;
              }
          }
          return false;
      }
      
  • 参考文献

    https://www.hrwhisper.me/leetcode-increasing-triplet-subsequence/

版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode 334. Increasing Triplet Subsequence

前言写这篇文章的主要目的是为了熟悉一下MarkDown这个编辑方式。因为昨天有道云笔记发布对MarkDown的支持,后知后觉的我才发现了这个编辑器,因此打算写一篇小算法来练练手。这个算法也不算难,虽然...

LeetCode#334. Increasing Triplet Subsequence

334. Increasing Triplet Subsequence Given an unsorted array return whether an increasing subseq...

334. Increasing Triplet Subsequence

Task: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in...

leedCode 334 Increasing Triplet Subsequence

问题描述: Given an unsorted array return whether an increasing sub sequence of length 3 exists or not...

Increasing Triplet Subsequence

题目描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or no...

LeetCode Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. ...

leetcode 300. Longest Increasing Subsequence

Given an unsorted array of integers, find the length of longest increasing subsequence. For examp...

LeetCode 题解(Week6):300. Longest Increasing Subsequence

原题目Given an unsorted array of integers, find the length of longest increasing subsequence.For exampl...

LeetCode 300.Longest Increasing Subsequence 在一维数组中找最长序列(好题)

题目 Given an unsorted array of integers, find the length of longest increasing subsequence. For...

LeetCode 300: Longest Increasing Subsequence

最长增加的子序列问题,归于动态规划问题。分析:从最后面的数往前去找。假设串为[10,9,2,5,3,7,101,18],我们从最后一个数18开始找,那么18这个数我们到底需不需要第一步就将它加入到最长...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode_334 Increasing Triplet Subsequence
举报原因:
原因补充:

(最多只允许输入30个字)