一天一道LeetCode系列

(一)题目

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.

(二)解题

本题的关键在于利用stack,想到了stack就不难写出代码了。

每次碰到(,{,[就压栈,碰到),},],就判断栈顶元素匹配上就出栈,匹配不上就返回false.

下面是Accepted的代码,复杂度为0(n):

class Solution {
public:
    bool isValid(string s) {
        stack<char> tmp;
        for(int i = 0 ; i < s.length() ; i++)
        {
            if(s[i] == '(' || s[i] == '{' || s[i] == '[')
            {
                tmp.push(s[i]);
            }
            else if(s[i] == ')' || s[i] == '}' || s[i] == ']')
            {
                if(tmp.empty()) return false;
                switch(s[i])
                {
                case ')':
                    if(tmp.top() == '(') tmp.pop();
                    else return false;
                    break;
                case ']':
                    if(tmp.top() == '[') tmp.pop();
                    else return false;
                    break;
                case '}':
                    if(tmp.top() == '{') tmp.pop();
                    else return false;
                    break;
                }
            }
        }
        if(tmp.empty()) return true;
        else return false;
    }
};

【一天一道LeetCode】#20. Valid Parentheses的更多相关文章

  1. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  2. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  3. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

    Valid Parentheses  Given a string containing just the characters '(', ')', '{', '}', '[' and ']', de ...

  6. leetcode 20 Valid Parentheses 括号匹配

    Given a string containing just the characters '(', ')', '{', '}', '[' and']', determine if the input ...

  7. [LeetCode] 20. Valid Parentheses ☆

    转载:https://leetcode.windliang.cc/leetCode-20-Valid%20Parentheses.html 描述 Given a string containing j ...

  8. LeetCode 20 -- Valid Parentheses

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  9. Java [leetcode 20]Valid Parentheses

    题目描述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  10. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

随机推荐

  1. ejabberd为游戏免除注册限制

    ejabberd为游戏免除注册限制 (金庆的专栏 2016.11) ejabberd聊天服务器默认会限制同一IP注册帐号须间隔600s. 在游戏中需要为每个角色注册一个聊天帐号,不应该有此限制. 可以 ...

  2. Objective-C语法概述

    Objective-C语法概述 简称OC 面向对象的C语言 完全兼容C语言 可以在OC里面混入C/C++代码 可以开发IOS和Mac OS X平台应用 语法预览 关键字 基本上都是以@开头(为了与C语 ...

  3. PGM:部分观测数据

    http://blog.csdn.net/pipisorry/article/details/52599451 基础知识 数据缺失的三种情形: 数据的似然和观测模型 Note: MLE中是将联合概率P ...

  4. PGM:部分有向模型之条件随机场与链图模型

    http://blog.csdn.net/pipisorry/article/details/52529287 贝叶斯网与马尔可夫网 [PGM:无向图模型:马尔可夫网]中例3.8和例4.8显示,贝叶斯 ...

  5. Android开发艺术探索——第二章:IPC机制(中)

    Android开发艺术探索--第二章:IPC机制(中) 好的,我们继续来了解IPC机制,在上篇我们可能就是把理论的知识写完了,然后现在基本上是可以实战了. 一.Android中的IPC方式 本节我们开 ...

  6. SpriteKit中的共享动作(Sharing Actions)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在SpriteKit中某些动作需要一些额外的延时,如果每次都重 ...

  7. android 网络工具 之Android-Volley的demo

    1.今天详细的研究了Volley的使用,下面来给大家介绍一下: Android Volley 是Google开发的一个网络lib,可以让你更加简单并且快速的访问网络数据.Volley库的网络请求都是异 ...

  8. Android控制软键盘的弹出和隐藏

    弹出软键盘 前提:必须要有一个可以编辑的控件(EditText),并且当前已经获取焦点 /** * 弹出软键盘 */ public void openKeyboard(View view) { // ...

  9. GDAL 2.0版本RPC校正速度测试

    GDAL2.0版本的更新日志中提到了对RPC校正的优化,今天测试了一下,发现提升的速度还是蛮快的,测试的数据是一个IRS-P5的数据. 单线程测试 首先使用一个线程进行测试,使用下面的批处理进行运行, ...

  10. 剑指Offer——银行网申内容模版

    剑指Offer--银行网申内容模版 年获得"优秀共青团员"称号 2013.12 科技标兵 2013.10 三好学生 2013.10 "三下乡"优秀学生 2013 ...