标签:height rac 需要 [] line inpu back ring min
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 class Solution { public boolean isValid(String s) { Stack<Character> stack=new Stack<>(); for(char c:s.toCharArray()){ if(c==‘(‘||c==‘{‘||c==‘[‘){ stack.push(c); } if(c==‘)‘){ if(stack.isEmpty()||stack.pop()!=‘(‘){ return false; } } if(c==‘}‘){ if(stack.isEmpty()||stack.pop()!=‘{‘) { return false; } } if(c==‘]‘){ if(stack.isEmpty()||stack.pop()!=‘[‘){ return false; } } } return stack.isEmpty(); } }
思路:
if(stack.isEmpty()||stack.pop()!=‘(‘){
20. Valid Parentheses【leetcode】
标签:height rac 需要 [] line inpu back ring min
原文地址:http://www.cnblogs.com/haoHaoStudyShare/p/7309527.html