leetcode 栈 括号匹配

https://oj.leetcode.com/problems/valid-parentheses/

遇到左括号入栈,遇到右括号出栈找匹配,为空或不匹配为空,

public class Solution {
public boolean isValid(String s) {
char c[]=s.toCharArray();
Stack<Character> stack=new Stack<Character>();
for(int i=0;i<s.length();i++)
{
if(c[i]=='('||c[i]=='['||c[i]=='{')stack.push(c[i]);
else
{
if(stack.isEmpty()) return false;
char c2=stack.peek();
if(c[i]==')'&&c2=='(') stack.pop();
else if(c[i]==']'&&c2=='[') stack.pop();
else if(c[i]=='}'&&c2=='{') stack.pop();
else return false;
} }
if(stack.isEmpty())
{
return true;
}
return false; } }
上一篇:Add AI feature to Xamarin.Forms app


下一篇:linux下工具exfs用法