1.问题描述

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.

2.解法分析

典型的栈用法。

class Solution {

public:

    bool isValid(string s) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        vector<char> myStack;

        map<char,char>dict;

        dict.insert(make_pair('(',')'));

        dict.insert(make_pair('[',']'));

        dict.insert(make_pair('{','}'));

        

        for(int i=0;i<s.length();++i)

        {

            if(s[i]=='('||s[i]=='['||s[i]=='{')myStack.push_back(s[i]);

            else

                if(s[i]==')'||s[i]==']'||s[i]=='}')

                {

                    if(myStack.empty())return false;

                    if(dict[myStack.back()]==s[i])myStack.pop_back();

                    else return false;

                }

        }

        

        if(myStack.empty())return true;

        return false;

        

    }

};

leetcode—Valid Parentheses的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode: Valid Parentheses 解题报告

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

  3. [Leetcode] valid parentheses 有效括号对

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

  4. LeetCode——Valid Parentheses

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

  5. [LeetCode] Valid Parentheses

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

  6. Python3解leetcode Valid Parentheses

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

  7. leetcode Valid Parentheses python

    # 解题思路: # 创建一个字典映射关系 dicts# 使用一个栈stk 遍历字符串s 得到一个新的字符串curItem 如果lastItem在dicts中的value和它相等 不做任何操作# 如果不 ...

  8. LeetCode Valid Parentheses 有效括号

    class Solution { public: void push(char c){ //插入结点 struct node *n=new struct node; n->nex=; n-> ...

  9. Valid Parentheses [LeetCode 20]

    1- 问题描述 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...

随机推荐

  1. :Hibernate逍遥游记-第16管理session和实现对话

    1. package mypack; public class Monkey{ private Long id; private String name; private int count; pri ...

  2. Photoshop:制作金属质感-不锈钢纹理

    效果图 过程: 1.滤镜->渲染->云彩 2.滤镜->模糊->高斯模糊 3.滤镜->杂色->添加杂色 ,数量10 4.滤镜->模糊->径向模糊 5.滤镜 ...

  3. [cocoapods]cocoapods问题解决

    错误1. While executing gem no such name 错误原因:gem 网址被挡住了. 解决办法:设置https://ruby.taobao.org/ 详情参考 http://w ...

  4. Android 在Intent中传递接口

    总结:在Activity中不能用intent传递匿名接口,原因如下:Activity A中生成了匿名接口M, 这个接口的引用就在组Activity A中,Activity A会禁止接口M 序列化.因为 ...

  5. 3、REST风格的URL

    1.概述 HTTP协议里面,四个表示操作方式的动词:GET.POST.PUT.DELETE,它们分别对应四种基本的操作,GET用来获取资源,POST用来新建资源,PUT用来更新资源,DELETE用来删 ...

  6. servlet request.getParamter 有时获取参数为null

    他妈的,参数有时可以获取,有时又不行,折腾了好久,把tomcat换成8.0的,之前用apache-tomcat-7.0.67

  7. xmlsechema验证

                   //创建xmlDocument                 XmlDocument doc = new XmlDocument();                  ...

  8. C#实现GDI+基本图的缩放、拖拽、移动

    C#实现GDI+基本图的缩放.拖拽.移动示例代码如下: using System;using System.Collections.Generic;using System.ComponentMode ...

  9. gulp自动刷新插件

    gulp自动刷新的插件很多,但是感觉最好用的还是 browser-sync 插件.如果不想用命令行,也可以使用 browser-sync界面工具 先安装 browser-sync 插件: npm in ...

  10. ASIFormDataRequest实现post的代码示例

    用jquery实现的Post方法可能如下 var param = $.param({ data: JSON.stringify({"from":"234",&q ...