LeetCode20:Valid Parentheses,parenthesis

來源:互聯網
上載者:User

LeetCode20:Valid Parentheses,parenthesis

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.

The brackets must close in the correct order, “()” and “()[]{}” are all valid but “(]” and “([)]” are not.

Hide Tags Stack String

這道題是檢驗輸入的字串是否符合規則,因為這是stack裡的一道題目,所以一開始就是衝著如何用棧來求解這個問題。
最開始想的是將字串中的元素全部插入棧中,然後再找出判斷規則,很快就發現行不通,因為棧無法進行遍曆操作,它只提供有限的幾個進棧出棧的函數,然後順著棧能進行的操作思考是不是在將字串中的資料入棧的過程中就能進行判斷呢?在紙上畫了下草圖發現真可以,每當遍曆到一個元素時將它與棧頂元素進行判斷,如果它和棧頂元素匹配,那麼將棧頂元素彈出,否則將它入棧。比如說對於輸入”([])”,一開始:(“進棧,然後由於”[“與”(“不匹配所以也進棧,然後遍曆到”]”,由於”]”與棧頂元素”[“匹配所以”[“出棧,這樣一直遍曆下去。。。。
runtime:0ms

class Solution {public:    bool isValid(string s) {        stack<char> datas;        for(int i=0;i<s.size();i++)        {            if(datas.empty())                datas.push(s[i]);            else            {                if(com(datas.top(),s[i]))                    datas.pop();                else                    datas.push(s[i]);            }        }        return datas.empty();    }    bool com(char lhs,char rhs)    {        if(lhs=='('&&rhs==')')            return true;        if(lhs=='['&&rhs==']')            return true;        if(lhs=='{'&&rhs=='}')            return true;        return false;    }};

在Discuss中看到了另外一種實現,基本思路和我上面也是一樣的,但是它的匹配不是像上面定義一個cmp函數,而是通過switch語句進行的,感覺也蠻不錯的,所以也按照它的思路實現了一遍:

 bool isValid(string s) {        stack<int> data;        for(int c:s)        {            switch(c)            {                case '(':                case '[':                case '{':data.push(c); break;                case ')':                {                    if(data.empty()||data.top()!='(')                        return false;                    else                    {                        data.pop();                        break;                    }                }                case ']':                {                    if(data.empty()||data.top()!='[')                        return false;                    else                    {                        data.pop();                        break;                    }                }                case '}':                {                    if(data.empty()||data.top()!='{')                        return false;                    else                    {                        data.pop();                        break;                    }                }            }        }        return data.empty();    }

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.