20.Valid Parentheses with stack

20.Valid Parentheses with stack_第1张图片

#include 
#include 
#include 
#include 

using namespace std;

class Solution {
public:
    bool isVaild(string s){
        stack<char> stack;
        for(int i = 0;i < s.size();i++){
            if(s[i] == '(' || s[i] == '{' || s[i] == '['){
                stack.push(s[i]);
            }else{
                if(stack.size() == 0){
                    return false;
                }
                char c = stock.top();
                stock.pop();

                char match;
                if(s[i] == ')')
                    match = '(';
                else if(s[i] == '}')
                    match = '{';
                else(assert( s[i] == ']'))
                    match = '[';
               }
               if(c != match)
                return false;
        }
        if(stack.size() != 0){
            return false;
        }
        return true;
    }

};

20.Valid Parentheses with stack_第2张图片

20.Valid Parentheses with stack_第3张图片

20.Valid Parentheses with stack_第4张图片
20.Valid Parentheses with stack_第5张图片
20.Valid Parentheses with stack_第6张图片

你可能感兴趣的:(算法)