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 "(]" and "([)]" are not.

题目比较简单,一看就知道对栈的考查。不多说,上代码。

bool isValid(string s)
{
    if(s.empty())
        return true;
    if(s.size()%2 == 1)
        return false;
    stack<char> st;
    for(int i =0;i<s.size();i++)
    {
        char tmp=s[i];
        if(tmp==‘(‘ || tmp==‘[‘ || tmp==‘{‘ )
        {
            st.push(tmp);
        }
        else
        {
            if(st.empty())
                return false;
            if( (tmp==‘)‘&&st.top()==‘(‘) ||
                (tmp==‘]‘&&st.top()==‘[‘) ||
                (tmp==‘}‘&&st.top()==‘{‘)
                )
                st.pop();
            else
                return false;
        }
        if(st.empty())
            return true;
    }
}

再看别人写的,利用自动变量,鲁棒性比较好,代码比较精炼。

bool isValid(string s)
{
    string left = "([{", right = ")]}";
    stack<char> stk;
    for (auto c : s) {
        if (left.find(c) != string::npos) {
            stk.push(c);
        }
        else {
            if (stk.empty() || stk.top() != left[right.find(c)])
                return false;
            else
                stk.pop();
        }
    }
    return stk.empty();
}
时间: 2024-10-13 00:18:06

LeetCode【20】Valid Parentheses的相关文章

leetcode第20题--Valid Parentheses

Problem: 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 "

LeetCode【125】Valid Palindrome

Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you consider that th

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 "(]" and "([)]&q

【leetcode系列】Valid Parentheses

很经典的问题,使用栈来解决,我这里自己实现了一个栈,当然也可以直接用java自带的Stack类. 自己实现的栈代码: import java.util.LinkedList; class StackOne { LinkedList<Object> data; int top; int maxSize; StackOne(int size) { // TODO Auto-generated constructor stub top = -1; maxSize = 100; data = new

LeetCode:有效的括号【20】

LeetCode:有效的括号[20] 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认为是有效字符串. 示例 1: 输入: "()" 输出: true 示例 2: 输入: "()[]{}" 输出: true 示例 3: 输入: "(]" 输出: false 示例 4: 输入: "

【LeetCode算法】Valid Parentheses

LeetCode第20题 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 closed i

[Leetcode][Python]32: Longest Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 32: Longest Valid Parentheseshttps://oj.leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')',find the length of the longest valid (well-fo

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 longest valid parentheses substring is "()" Example

【20】高度最小的BST

[题目] 对于一个元素各不相同且按升序排列的有序序列,请编写一个算法,创建一棵高度最小的二叉查找树. 给定一个有序序列int[] vals,请返回创建的二叉查找树的高度. [代码] import java.util.*; public class MinimalBST { public int buildMinimalBST(int[] vals) { if(vals == null || vals.length <1) return 0; return buildMinimalBSTCore(