class Solution {
public boolean isValid(String s) {
List<Character> stack=new ArrayList<>();
if (s.length()%2!=0)
return false;
if (s.length()==0)
return true;
for (int i=0;i<s.length();i++){
char c=s.charAt(i);
if(c=='('||c=='['||c=='{'){
stack.add(c);
continue;
}
if(c==')'){
if(stack.size()<1 ||stack.get(stack.size()-1)!='(')
return false;
stack.remove(stack.size()-1);
}
if(c==']'){
if(stack.size()<1 ||stack.get(stack.size()-1)!='[')
return false;
stack.remove(stack.size()-1);
}
if(c=='}'){
if(stack.size()<1 ||stack.get(stack.size()-1)!='{')
return false;
stack.remove(stack.size()-1);
}
}
if(stack.size()>0)
return false;
return true;
}
}
Java中长度回顾
1,数组:length属性
2,String:length()
3,泛型集合:size()