public int scoreOfParentheses(String s) {
Deque<Integer> d = new ArrayDeque<>();
d.addLast(0);
for (char c : s.toCharArray()) {
if (c == '(') //(用0占位
d.addLast(0);
else {//)则取出栈顶,若为(则+1,若为平衡字符串则*2
int cur = d.pollLast();
d.addLast(d.pollLast() + Math.max(cur * 2 , 1));
}
}
return d.peekLast();
}
栈leetcode.856
最新推荐文章于 2024-07-27 17:56:15 发布