【ceetcode】12 valid parentheses

有效的括弧匹配

注意事项:

1 使用stack

遇到‘(‘ ‘[‘ ‘{‘就push,遇到‘)‘ ‘]‘ ‘}‘匹配进行pop等操作

class Solution {
public:
    bool isValid(string s) {
        stack<char> mystack;
        char tmp;
        for(char c:s){
        switch(c){
            case ‘(‘:
            case ‘[‘:
            case ‘{‘:
                mystack.push(c);
                break;
            case ‘)‘:
                if(mystack.empty()||mystack.top()!=‘(‘)    
                    return false;
                else{
                    mystack.pop();
                    break;
                }
            case ‘]‘:
             if(mystack.empty()||mystack.top()!=‘[‘)    
                    return false;
                else{
                    mystack.pop();
                    break;
                }
            case ‘}‘:
             if(mystack.empty()||mystack.top()!=‘{‘)    
                    return false;
                else{
                    mystack.pop();
                    break;
                }
            default:
                break;
        }
           
        }//end for
       
        
        return mystack.empty();
           
    }
};

时间: 2024-10-20 20:55:32

【ceetcode】12 valid parentheses的相关文章

【LeetCode】Longest Valid Parentheses 解题报告

[题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example

【leetcode】Longest Valid Parentheses

Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length

【栈】Longest Valid Parentheses

题目:leetcode Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", whic

【LeetCode】20. Valid Parentheses

题目: 思路:用Stack基本操作完成. 要检测输入字符是否满足这个条件,一个非常合适的数据结构是stack,后进先出的特征正好满足检测的需求.在检测的时候,每次检查一个字符,如果是左括号,就入栈,如果是右括号,并且右括号和当前栈顶符号是左右配对的,那么就弹出栈顶并且进行下一次检测,如果不满足上面两种情况,就说明检查到了一个非法字符,返回false. public class Solution { public boolean isValid(String s) { if(s.length()=

【leetcode 】20. Valid Parentheses

这题需要注意: 栈的空指针 全局变量stack需要clear 输入结束时栈长度不为空 public class Solution { private static final Stack<Character> stack = new Stack<Character>(); public boolean isValid(String s) { stack.clear(); for (int i = 0; i < s.length(); ++i) { char c = s.cha

【leetcode刷题笔记】Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

【ThinkingInC++】12、宏处理定义

/** * 功能:宏处理定义 * 时间:2014年8月12日20:02:07 * 作者:cutter_point */ #include<iostream> #include<stdlib.h> using namespace std; #define PRINT(STR, VAR) cout<<STR "=" <<VAR<<endl; //这里宏定义之后,所有PRINT("STR", VAR) //都会被

【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Example 1:             Input: "(()"                         Output: 2                      Explanation: The long

【Android】12.2 利用Intent启动和关闭Activity

分类:C#.Android.VS2015: 创建日期:2016-02-23 一.简介 Android应用程序中一般都有多个Activity,在Activity中,通过调用StartActivity方法,并在该方法的参数中传递Intent对象,就可以实现不同Activity之间的切换和数据传递. 通过StartActivity方法传递intent对象来启动另一个Activity时,可分为两类: l 显式启动:在创建的Intent对象中明确指定启动的是哪个Activity: l 隐式启动:安卓系统根