Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. public int calPoints(String[] ops) {
  2. int sum = 0 ;
  3. Stack<Integer> valids = new Stack<>();
  4. for (String c : ops) {
  5. if(c.matches("-?\\d+"))
  6. {
  7. int temp = Integer.parseInt(c);
  8. sum += temp;
  9. valids.push(temp);
  10. }
  11. else
  12. {
  13. if(c == "C" || c == "c")
  14. {
  15. sum -= valids.pop();
  16. }
  17. else if(c == "D" || c == "d")
  18. {
  19. int peek = valids.peek();
  20. int temp = peek * 2;
  21. valids.push(temp);
  22. sum += temp;
  23. }
  24. else
  25. {
  26. int temp = valids.pop();
  27. int peek = valids.peek();
  28. sum += temp + peek;
  29. valids.push(temp);
  30. }
  31. }
  32. }
  33. return sum;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement