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.

题意:给定字符串判断是否为合法的括号对

思路:利用栈结构,遇到左括号((、[、{)都压入栈中,遇到右括号了,若栈为空,则肯定不合法;若栈不为空,则看其是否和栈顶字符向匹配。这里以最后栈是否为空来判断整个字符串是否合法,不然类似于((( [ ] )),就会合法的了。代码如下:

 class Solution {
public:
bool isValid(string s)
{
if(s.empty()) return true;
stack<char> stk;
for(int i=;i<s.size();++i)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')
stk.push(s[i]);
else
{
if( stk.empty())
return false;
char temp=stk.top();
stk.pop();
if((temp=='('&&s[i] !=')')||(temp=='['&&s[i] !=']')||(temp=='{'&&s[i] !='}'))
return false;
} }
return stk.empty();
}
};

括号的题还有generate parentheses 、longest valid parentheses

[Leetcode] valid parentheses 有效括号对的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

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

  2. LeetCode Valid Parentheses 有效括号

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

  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有效括号序列

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

  6. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. [LintCode] Valid Parentheses 验证括号

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

  8. LeetCode: Valid Parentheses 解题报告

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

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

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

随机推荐

  1. linux-centos6①

  2. tpo-08 C2 A strategy for attracting customers

    第 1 段 1.Listen to a conversation between a student and a business professor. 听一段学生和教授的对话. 第 2 段 1.So ...

  3. Http的请求和响应

    请求有客户端发起:可分为4个部分,请求方法(Requestmethod).请求的网址(Request URL).请求头(Request Headers).请求体(Request Body) 1.请求方 ...

  4. 小程序页面的四种文件(JSON、WXML、WXSS、JS)加载顺序

    一个小程序页面由四种文件组成: 1)json 页面配置文件 2)js 页面逻辑文件(必需) 3)wxml 页面结构文件(必需) 4)wxss 页面样式文件 这四个文件的加载顺序: 第一步: 加载页面j ...

  5. JAVA基础学习之路(十)this关键字

    class Book { String name; int price; int num;//构造方法之间的互相调用解决了代码的重复问题,但是一定要留出口 public Book() { ,); } ...

  6. 一键部署pxe环境

    系统:Centos6.5 环境:VMware Workstation12 #!/bin/bash # Please prepare CentOS ISO image first # root pass ...

  7. 1053 Path of Equal Weight (30 分)(树的遍历)

    题目大意:给出树的结构和权值,找从根结点到叶子结点的路径上的权值相加之和等于给定目标数的路径,并且从大到小输出路径 #include<bits/stdc++.h> using namesp ...

  8. 棋盘问题:dfs

    Description 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子 ...

  9. Python变量常量及注释

    一.变量命名规则1.有字母.数字.下划线搭配组合而成2.不能以数字开头,更不能全为数字3.不能用Python的关键字4.不要太长5.名字要有意义6.不要用中文7.区分大小写8.采用驼峰体命名(多个单词 ...

  10. opencv-学习笔记(3)

    opencv-学习笔记(3) 这章讲了 图像加法 opencv测试效率 IPYTHON测试效率 图像加法 cv2.add() 要求,两图片必须大小类型相同 然后是图像混合cv2.addWeighted ...