MCPcopy Index your code
hub / github.com/neetcode-gh/leetcode / calPoints

Method calPoints

java/0682-baseball-game.java:2–28  ·  view source on GitHub ↗
(String[] operations)

Source from the content-addressed store, hash-verified

1class Solution {
2 public int calPoints(String[] operations) {
3 Stack<Integer> st = new Stack<>();
4
5 for(String op : operations) {
6 if(op.equals("+") && st.size() >= 2) {
7 int score1 = st.pop();
8 int score2 = st.peek();
9 int score3 = score1 + score2;
10 st.push(score1);
11 st.push(score3);
12 } else if(op.equals("D") && !st.isEmpty()) {
13 int score = st.peek();
14 st.push(score*2);
15 } else if(op.equals("C") && !st.isEmpty()) {
16 st.pop();
17 } else {
18 st.push(Integer.parseInt(op));
19 }
20 }
21
22 int sum = 0;
23 while(!st.isEmpty()) {
24 sum += st.pop();
25 }
26
27 return sum;
28 }
29}

Callers

nothing calls this directly

Calls 6

equalsMethod · 0.80
sizeMethod · 0.45
popMethod · 0.45
peekMethod · 0.45
pushMethod · 0.45
isEmptyMethod · 0.45

Tested by

no test coverage detected