码迷,mamicode.com
首页 > 其他好文 > 详细

Valid Parentheses_LeetCode

时间:2017-10-16 13:43:23      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:count   ring   tco   html   color   class   nta   des   push   

Description:

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.

 

解题思路:

详细参见之前博客

http://www.cnblogs.com/SYSU-Bango/p/6306962.html

 

代码:

class Solution {
public:
    bool isValid(string str) {
        int count = 0;
        int size = str.length();
        stack<char> temp;
        for (int i = 0; i < size; i++) {
            if (str[i] == ( || str[i] == [ || str[i] == {) {
                temp.push(str[i]);
            } else {
                if (str[i] == )) {
                    if (!temp.empty() && temp.top() == () {
                        temp.pop();
                    } else {
                        //cout << "No" << endl;
                        return false;
                        count = 1;
                        break;
                    }
                }
                if (str[i] == }) {
                    if (!temp.empty() && temp.top() == {) {
                        temp.pop();
                    } else {
                        //cout << "No" << endl;
                        return false;
                        count = 1;
                        break;
                    }
                }
                if (str[i] == ]) {
                    if (!temp.empty() && temp.top() == [) {
                        temp.pop();
                    } else {
                        //cout << "No" << endl;
                        return false;
                        count = 1;
                        break;
                    }
                }
            }
        }
        if (count == 0) {
            if (temp.empty() == true) {
            //cout << "Yes" << endl; 
                return true;
        } else {
        //cout << "No" << endl;
                return false;
    }
        }
    }
};


        
        

 

Valid Parentheses_LeetCode

标签:count   ring   tco   html   color   class   nta   des   push   

原文地址:http://www.cnblogs.com/SYSU-Bango/p/7676350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!