Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int calPoints(String[] ops) {
- int sum = 0 ;
- Stack<Integer> valids = new Stack<>();
- for (String c : ops) {
- if(c.matches("-?\\d+"))
- {
- int temp = Integer.parseInt(c);
- sum += temp;
- valids.push(temp);
- }
- else
- {
- if(c == "C" || c == "c")
- {
- sum -= valids.pop();
- }
- else if(c == "D" || c == "d")
- {
- int peek = valids.peek();
- int temp = peek * 2;
- valids.push(temp);
- sum += temp;
- }
- else
- {
- int temp = valids.pop();
- int peek = valids.peek();
- sum += temp + peek;
- valids.push(temp);
- }
- }
- }
- return sum;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement