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.

翻译:

给一个字符串,判断括号是否匹配成功。

思路:

用一个字符栈,读到一个字符时判断,如果栈顶和当前这个字符是满足左右括号匹配,则弹出,否则压栈。

如果最后栈为空,说明匹配成功。

代码:

 public static boolean isValid(String s) {
		if(s.length() == 0 )
			return true;
		Stack<Character> st = new Stack<Character>();
		st.push(s.charAt(0));
		for(int i = 1 ; i< s.length();i++)
		{
			if(!st.empty()&& ismatch(st.peek(), s.charAt(i)))//非空且栈顶和当前字符匹配,弹出
				st.pop();
			else {
				st.push(s.charAt(i));
			}
		}
		if(st.empty())
			return true;
		return false;
    }

	public  static  boolean ismatch(char a,char b)
	{
		if((a=='('&&b==')')||(a=='{'&&b=='}')||(a=='['&&b==']'))
			return true;
		else
			return false;
	}
时间: 2024-10-18 17:11:49

LeetCode 20 Valid Parentheses 括号匹配问题的相关文章

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

20. Valid Parentheses括号匹配

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 "(]"

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

20. Valid Parentheses - 括号匹配验证

Description: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. Example: The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]&quo

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description 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 brac

leetCode 20. Valid Parentheses 字符串

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 "(]&quo

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

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 "(]" a

Java [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 "([)

Leetcode 20 Valid Parentheses stack的应用

判断括号是否合法 1.用stack存入左括号“([{”,遇到相应的右括号“)]}”就退栈 2.判断stack是否为空,可以判断是否合法 1 class Solution { 2 public: 3 bool isValid(string s) { 4 stack<char> sc; 5 char in[] ="([{"; 6 char out[] =")]}"; 7 for(string::size_type i = 0; i < s.size()