小代碼棧之合法性一點(diǎn)思路不一樣的際遇

/************
1 2 3 4 如棧
出棧 
3 2 4 1 返回0
錯(cuò)誤 3 1 2 4 返回-1
**********/
#include<iostream>
#include<stack>
#include<string.h>

using namespace std;

bool judge(const char *push_seq, const char *pop_seq)
{
    if (NULL == push_seq || NULL == pop_seq)
    {
        return false;         
    }
    if (strlen(push_seq) != strlen(pop_seq))
    {
        return false;
    }
   
    stack<char>sc;
    while (*push_seq)
    {
        if (0 == sc.size() || sc.top() != *pop_seq)
        { 
            sc.push(*push_seq++); //
        }
        else
        { 
            sc.pop();
            ++pop_seq; //
        }
    }
    while (sc.size())
    {
        if (sc.top() != *pop_seq++)
        {
            return false;
        }
        sc.pop();
    }
    
    return true;
}  
int mycheck1(int a[],int n)
{
    stack<int>s;
    int i,j,k=1;          // 3 2 4 1
    for(i=0;i<n;i++)
    {
        if(a[i]>k)
          { for(j=k;j<a[i];j++) s.push(j);
                k=j+1;cout<<k<<endl;
              continue;
             }
       
        if(a[i]==s.top()){s.pop();continue;}
        if(a[i]<s.top()) return -1;
    }
    cout<<endl;
    return 0;
}

int mycheck(int a[],int n)
{
    stack<int>s;      s.push(0);
    int i,j,k=1;         //BUG段  經(jīng)過(guò)分析 在于s.top段  因?yàn)闆](méi)有元素時(shí)這樣做有越界的含義
    for(i=0;i<n;i++)
    {
        if(a[i]>k)
          { for(j=k;j<a[i];j++) s.push(j);
                k=j+1;cout<<k<<endl;
              continue;
             }
       
        if(a[i]==s.top()){s.pop();continue;}
        if(a[i]<s.top()) return -1;
    }
    cout<<endl;
    return 0;
}
int main()
{
     //cout<<judge("1234","3241")<<endl;
     //int a[]={1,2,3,4};  
int a[]={3,2,4,1};
// int a[]={4,3,2,1};ok
      cout<<mycheck(a,4);

     
    return 0;
}

網(wǎng)頁(yè)名稱:小代碼棧之合法性一點(diǎn)思路不一樣的際遇
網(wǎng)站鏈接:http://bm7419.com/article26/jdsscg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、微信公眾號(hào)、品牌網(wǎng)站制作虛擬主機(jī)、網(wǎng)站設(shè)計(jì)公司、軟件開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名