Valid Parentheses leetcode java

题目

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.

题解:

这道题是一道很常见的老题,记得当时看严蔚敏的数据结构还有数据结构课上面都见过这道题,一道训练栈的基础题。

解题方法是:

一个个检查给的characters,如果是左括号都入栈;如果是右括号,检查栈如果为空,证明不能匹配,如果栈不空,弹出top,与当前扫描的括号检查是否匹配。

全部字符都检查完了以后,判断栈是否为空,空则正确都匹配,不空则证明有没匹配的。

注意:

检查字符是用==,检查String是用.isEqual(),因为String是引用类型,值相等但是地址可能不等。

代码如下:

1     public boolean isValid(String s) {
 2         if(s.length()==0||s.length()==1)
 3             return false;
 4 
 5         Stack<Character> x = new Stack<Character>();
 6         for(int i=0;i<s.length();i++){
 7             if(s.charAt(i)==‘(‘||s.charAt(i)==‘{‘||s.charAt(i)==‘[‘){
 8                 x.push(s.charAt(i));
 9             }else{
10                 if(x.size()==0)
11                     return false;
12                 char top = x.pop();
13                 if(s.charAt(i)==‘)‘)
14                     if(top!=‘(‘)
15                         return false;
16                 else if(s.charAt(i)==‘}‘)
17                     if(top!=‘{‘)
18                         return false;
19                 else if(s.charAt(i)==‘]‘)
20                     if(top!=‘[‘)
21                         return false;
22             }
23     }
24         return x.size()==0;
25 }

Valid Parentheses leetcode java

时间: 2024-10-05 05:06:09

Valid Parentheses leetcode java的相关文章

Longest Valid Parentheses leetcode java

题目: 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

Valid Number leetcode java

题目: Validate if a given string is numeric. Some examples: "0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => true Note: It is intended for the problem statement to be ambigu

Valid Palindrome leetcode java

题目: 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

Valid Sudoku leetcode java

题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku board (parti

Generate Parentheses leetcode java

题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 题解:

Valid Parentheses [LeetCode 20]

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

Longest Valid Parentheses Leetcode 32 一种奇特的解法

题目: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 i

Longest Valid Parentheses - LeetCode

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 &

LeetCode中Valid Parentheses的JAVA实现

先上题目: 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 "([)