Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package algo;
- public class Score {
- public static void main(String[] args) {
- String[] nums = { "5", "-2", "4", "Z", "X", "9", "+", "+" }; // 27
- // String[] nums = { "1", "2", "4", "Z"}; // 3
- System.out.println(findScore(nums, nums.length));
- }
- static int findScore(String[] blocks, int length) {
- int sum = 0;
- int[] score = new int[length];
- for (int i = 0; i < length; i++) {
- if (blocks[i].equals("Z")) {
- if (i == 0) {
- score[i] = 0;
- }
- if (i > 0) {
- score[i - 1] = 0;
- }
- if (i > 1) {
- score[i] = score[i - 2];
- score[i - 2] = 0;
- }
- } else if (blocks[i].equals("X")) {
- score[i] = score[i - 1] * 2;
- } else if (blocks[i].equals("+")) {
- score[i] = score[i - 1] + score[i - 2];
- } else {
- score[i] = (int) Integer.valueOf(blocks[i]);
- }
- }
- for (int i = 0; i < score.length; i++) {
- sum += score[i];
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement