ScoreParentheses
ScoreParentheses.java 源码
package datastructure.stack.leetcode;
import java.util.LinkedList;
/**
* @author roseduan
* @time 2021/2/14 9:13 上午
* @description 括号的分数
*/
public class ScoreParentheses {
public int scoreOfParentheses(String S) {
LinkedList<Integer> stack = new LinkedList<>();
for (char c : S.toCharArray()) {
if (c == '(') {
stack.addFirst(-1);
} else {
int k = 0;
while (stack.peekFirst() != -1) {
k += stack.pollFirst();
}
stack.pollFirst();
stack.addFirst(k == 0 ? 1 : 2 * k);
}
}
return stack.stream().mapToInt(Integer::intValue).sum();
}
}
你可能感兴趣的文章
0
赞
热门推荐
-
2、 - 优质文章
-
3、 gate.io
-
8、 golang
-
9、 openharmony
-
10、 Vue中input框自动聚焦