public class Solution {
public static boolean isValid(String s)
{
Stack<Character> stack = new Stack<Character>();
int N = s.length();
for(int i=0; i<N ; i++ )
{
char c = s.charAt(i);
if(c=='('||c=='{'||c=='[')
stack.push(c);
else if(c==')')
{
if (stack.isEmpty()||stack.pop()!='(')
return false;
}
else if(c=='}')
{
if (stack.isEmpty()||stack.pop()!='{')
return false;
}
else if(c==']')
{
if (stack.isEmpty()||stack.pop()!='[')
return false;
}
}
if(stack.isEmpty())
return true;
else
return false;
}
}
Valid Parentheses
最新推荐文章于 2021-02-23 15:41:56 发布