<LeetCode OJ> 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.

我的解法:时间复杂度O(N)

一旦出现左型括号,先存储起来就准备与右型括号配对,假设是右型括号,就运行配对(检查存储之中是否有左型括号)!

从标签来看,利用栈来完毕如此操作,左型括号先压栈等待匹配,

右型括号就查看栈顶是否是左型括号(假设不是已经不匹配了。"([)]" are not.)

最后通过栈是否是空的来推断是否是全然配对

class Solution {
public:
    bool isValid(string str) {
        stack<char> s;
        if(str.size() <= 1)
             return false;
        for(int i=0;i<str.size();i++)
        {
            switch(str[i])
            {
                //左型括号直接压栈
                case '(' :
                 s.push('(');
                 break;
                case '{' :
                 s.push('{');
                 break;
                case '[' :
                 s.push('[');
                 break;

                 //右型括号运行匹配操作,能匹配则弹出栈顶,不能则返回false
                case ')' :
                 if(s.empty())
                 {
                     return false;
                 }else{
                     char ch=s.top();
                     if(ch=='(')
                      {
                          s.pop();
                          continue;
                      }else{
                          return false;
                          break;
                      }
                 }
                 break;

                 case '}' :
                 if(s.empty())
                 {
                     return false;
                 }else{
                     char ch=s.top();
                     if(ch=='{')
                      {
                          s.pop();
                          continue;
                      }else{
                          return false;
                          break;
                      }
                 }
                 break;

                 case ']' :
                 if(s.empty())
                 {
                     return false;
                 }else{
                     char ch=s.top();
                     if(ch=='[')
                      {
                          s.pop();
                          continue;
                      }else{
                          return false;
                          break;
                      }
                 }
                 break;
            }
        }
        //最后若是空则完毕匹配
        if(s.empty())
            return true;
        else
             return false;
    }
};

学习别人的算法:

运用数据结构红黑树map来存储配对的括号,以便查找匹配情况(时间浮渣度N*lg(N))

一旦出现左型括号,就存储相应的右型括号,

假设是右型括号,就运行配对(检查存储之中是否有左型括号)!

假设匹配则删除vector的最后一个元素

最后通过vc是否是空的来推断是否是全然配对

class Solution {
public:
    bool isValid(string s)
    {
        vector<char> vc;
        map<char, char> mapping;
        mapping['('] = ')';
        mapping['{'] = '}';
        mapping['['] = ']';
        for (int i = 0; i < s.length(); ++i)
        {
            if (s[i] == '(' || s[i] == '{' || s[i] == '[') //假设是左型括号。就直接压入相应的右型括号到vector
                vc.push_back(mapping[s[i]]);
            else if (vc.empty() || vc.back() != s[i])//假设右型括号的不匹配情况
                return false;
            else         //右型括号的匹配情况
                vc.pop_back();//删除vector中的最后一个元素
        }
        return vc.empty() ?

true : false;
    }
};

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50380804

原作者博客:http://blog.csdn.net/ebowtang

时间: 2024-08-08 05:35:57

&lt;LeetCode OJ&gt; 20. Valid Parentheses的相关文章

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

LeetCode记录之20——Valid Parentheses

本题主要是找是否有匹配的字符串,因为还没有复习到栈之类的知识点,只能还是采用暴力方法了,后期会补上更加优化的算法.我的思路就是先遍历一遍找是否有匹配的符号,有的话就删除,然后继续遍历,直至结束. Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the co

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

20. Valid Parentheses【leetcode】

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 "(]&quo

leetCode 20. Valid Parentheses 字符串

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 "(]&quo

20. Valid Parentheses括号匹配

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 "(]"

20. Valid Parentheses(js)

20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be

刷题20. Valid Parentheses

一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop.top,. 我总共提交了3次: 第1次:Runtime Error,错误原因在于pop的时候,未判断栈是否为空. 第2次:Wrong Answer,这个是"眼大"疏忽导致的,我写的时候只考虑了()[]未考虑{}. 第3次:终于正确了,性能还可以: Runtime: 0 ms, faster

[Leetcode][Python]20: Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 20: Valid Parentheseshttps://oj.leetcode.com/problems/valid-parentheses/ Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.T