Advertisement
Guest User

Untitled

a guest
Mar 20th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package algo;
  2.  
  3. public class Score {
  4.  
  5. public static void main(String[] args) {
  6. String[] nums = { "5", "-2", "4", "Z", "X", "9", "+", "+" }; // 27
  7. // String[] nums = { "1", "2", "4", "Z"}; // 3
  8. System.out.println(findScore(nums, nums.length));
  9. }
  10.  
  11. static int findScore(String[] blocks, int length) {
  12. int sum = 0;
  13. int[] score = new int[length];
  14.  
  15. for (int i = 0; i < length; i++) {
  16.  
  17. if (blocks[i].equals("Z")) {
  18. if (i == 0) {
  19. score[i] = 0;
  20. }
  21. if (i > 0) {
  22. score[i - 1] = 0;
  23. }
  24. if (i > 1) {
  25. score[i] = score[i - 2];
  26. score[i - 2] = 0;
  27. }
  28. } else if (blocks[i].equals("X")) {
  29. score[i] = score[i - 1] * 2;
  30. } else if (blocks[i].equals("+")) {
  31. score[i] = score[i - 1] + score[i - 2];
  32. } else {
  33. score[i] = (int) Integer.valueOf(blocks[i]);
  34. }
  35. }
  36.  
  37. for (int i = 0; i < score.length; i++) {
  38. sum += score[i];
  39. }
  40.  
  41. return sum;
  42. }
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement