zoukankan      html  css  js  c++  java
  • 20. Valid Parentheses

    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.


    模拟操作

    #include <iostream>
    #include <vector>
    #include <set>
    #include <algorithm>
    #include <string>
    #include <sstream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    using namespace std;
    
    class Solution {
    public:
    	bool isValid(string s) {
    		if (s.size() == 1)
    		{
    			return false;
    		}
    		stack<char>sta;
    		char ch;
    		for (auto iter = s.begin(); iter != s.end(); iter++)
    		{
    			ch = *iter;
    			if (ch == '(' || ch == '{' || ch == '[')
    			{
    				sta.push(ch);
    			}
    			else
    			{
    				if (sta.size() == 0)
    				{
    					return false;
    				}
    				ch = sta.top();
    				sta.pop();
    				switch (*iter)
    				{
    				case ')':
    					if (ch != '(')
    					{
    						return false;
    					}
    					break;
    				case '}':
    					if (ch != '{')
    					{
    						return false;
    					}
    					break;
    				case ']':
    					if (ch != '[')
    					{
    						return false;
    					}
    					break;
    				}
    			}
    		}
    		if (sta.size() != 0)
    		{
    			return false;
    		}
    		return true;
    	}
    };
    
    int main()
    {
    	Solution s;
    	cout << s.isValid("[[)") << endl;
    	return 0;
    }


    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    Form表单提交数据的几种方式
    前端基础-HTML
    python入门函数详解
    Python作业编写
    Python入门数据类型详解
    Jquery选择器
    做外链接和有外链接区别
    三层架构
    drop,delete,truncate区别
    run()和star()区别
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616319.html
Copyright © 2011-2022 走看看