zoukankan      html  css  js  c++  java
  • 【LeetCode】456. 132 Pattern

    Given a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such that i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.

    Note: n will be less than 15,000.

    Example 1:

    Input: [1, 2, 3, 4]
    
    Output: False
    
    Explanation: There is no 132 pattern in the sequence.
    

    Example 2:

    Input: [3, 1, 4, 2]
    
    Output: True
    
    Explanation: There is a 132 pattern in the sequence: [1, 4, 2].
    

    Example 3:

    Input: [-1, 3, 2, 0]
    
    Output: True
    
    Explanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].


    题意:找出给出的数组是否符合132模式,即i<j<k时,是否有a[i]<a[k]<a[j]
    这个题我是没有思路的,所以只有使用暴力了,但是还是提交了很多次才ac,果然昏睡状态下是不适合做题的。
    暴力破解思路:
      每循环到一个值a[j]的时候,找出这个值前面的最小值a[i],同时找出这个值后面的符合条件的值a[k]
    ps:这个题让人很受伤,1750ms
    bool find132pattern(int* nums, int numsSize) {
        int i;
        int p=0,q=0;
        int lmin=nums[0],flag;
        for(i=1;i<numsSize-1;i++)
        {
            flag=0;
            for(;p<i;p++)      //这里要保存p和lmin的状态,否则会超时不能ac,都是泪
                if(lmin>nums[p])
                    lmin=nums[p];
            for(q=numsSize-1;q>i;q--)
                if(lmin<nums[q]&&nums[q]<nums[i])
                    {
                        flag=1;
                        break;
                    }
            if(flag&&nums[q]<nums[i])
                return true;
        }
        return false;
    }

     看到了另外一种解法,9ms

    思路:

      逆遍历整个数组,维护一个已遍历的第二大的数,维护一个存放最大的数的数组,当出现比第二大的数小的数的时候,返回True

    C代码:

     1 bool find132pattern(int* nums, int numsSize) {  
     2   int second=INT_MIN;
     3   int tmp[numsSize];
     4   int i,len=0;
     5   for(i=numsSize-1;i>=0;i--)
     6   {
     7       if(nums[i]<second)
     8         return true;            //当出现比第二大的数小的时候,返回True
     9       while(len>0&&nums[i]>tmp[len-1])
    10         second=tmp[--len];         //获得比新出现的最大数小的第二大的数
    11       tmp[len++]=nums[i];
    12   }
    13   return false;
    14 }

     Python代码:

     1 class Solution(object):
     2     def find132pattern(self, nums):
     3         """
     4         :type nums: List[int]
     5         :rtype: bool
     6         """
     7         second = -10000000000000     #不知道怎么设置Python中的最小整形值
     8         st = []
     9         for num in nums[::-1]:
    10             if num<second:
    11                 return True
    12             while st and num>st[-1]:
    13                 second=st.pop()
    14             st.append(num)
    15         return False
    16         
  • 相关阅读:
    嵌入式linux驱动开发之点亮led(驱动编程思想之初体验)
    嵌入式Linux驱动开发之helloword心得
    PJMEDIA之录音器的使用(capture sound to avi file)
    PJMEID学习之视频的捕捉与播放
    PJSIP-PJMEDIA【使用pjmedia 播放wav格式的音乐】
    PJSIP-PJLIB-Socket
    PJSIP-PJLIB(samples) (the usage of the pjlib lib) (eg:string/I/O)
    Start with PJSIP on windows
    Android实际开发之网络请求组件的封装(OkHttp为核心)
    Android实际开发中的首页框架搭建(二、首页框架实现)
  • 原文地址:https://www.cnblogs.com/fcyworld/p/6160687.html
Copyright © 2011-2022 走看看