标签:
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 { /** * @param s A string * @return whether the string is a valid parentheses */ public boolean isValidParentheses(String s) { if(s==null) return false; if(s.length()%2 !=0) return false; // Write your code here char[] c=s.toCharArray(); for(int i=1;i<s.length();i++) { boolean r1= c[i-1]==‘(‘ && c[i]==‘)‘; boolean r2= c[i-1]==‘{‘ && c[i]==‘}‘; boolean r3= c[i-1]==‘[‘ && c[i]==‘]‘; if(r1||r2||r3) { String s1=s.substring(0,i-1)+s.substring(i+1); if(s1.length()==0) return true; else return isValidParentheses(s1); } } return false; } }
[lintcode easy]Valid Parentheses
标签:
原文地址:http://www.cnblogs.com/kittyamin/p/5009007.html