題目: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> temp=new Stack<character>(); boolean flag=true; for(int i=0;i<s.length();i++) switch="" case="" :="" flag="false;" default:="" return="" else="" pre=""><p> </p> </s.length();i++)></character></character>