抽取公共方法,重构代码逻辑

This commit is contained in:
wanglihui
2020-07-08 19:44:46 +08:00
parent 78664828e1
commit 0e926aa7d0
25 changed files with 911 additions and 1052 deletions

View File

@@ -106,4 +106,25 @@ public class TestMap {
}
public boolean isValid(String s) {
HashMap<Character, Character> map = new HashMap<>();
map.put('(',')');
map.put('[',']');
map.put('{','}');
Stack<Character> stack = new Stack<Character>();
for (int i = 0;i < s.length();i++){
Character c = s.charAt(i);
if (map.containsKey(c)){
Character c1 = stack.isEmpty() ? '#' : stack.pop();
if (map.get(c) != c1){
return false;
}
}else {
stack.push(c);
}
}
return stack.isEmpty();
}
}