@Test public void noSlicesWhenArraySmallerThan4() throws Exception { Solution solution = new Solution(); int slices = solution.numberOfArithmeticSlices(new int[]{}); assertThat(slices, is(0)); }
public int numberOfArithmeticSlices(int[] A) { if (A.length < 3) { return 0; } else { int prev = A[1]; int diff = A[1] - A[0]; int continuous = 2; int slices = 0; for (int i = 2; i < A.length; i++) { int current = A[i]; int newDiff = current - prev; boolean isContinuous = newDiff == diff; if (isContinuous) { continuous++; } if (!isContinuous || i == A.length - 1) { slices += slicesOf(continuous); continuous = 2; } diff = newDiff; prev = current; } return slices; } }
Solution { public int numberOfArithmeticSlices(int[] A) { if (A.length < 3) { return 0; } else { int prev = A[1]; int diff = A[1] - A[0]; int continuous = 2; int slices = 0; for (int i = 2; i < A.length; i++) { int current = A[i]; int newDiff = current - prev; boolean isContinuous = newDiff == diff; if (isContinuous) { continuous++; } if (!isContinuous || i == A.length - 1) { slices += slicesOf(continuous); continuous = 2; } diff = newDiff; prev = current; } return slices; } } }
Solution { public int numberOfArithmeticSlices(int[] A) { if (A.length < 3) { return 0; } else { int prev = A[1]; int diff = A[1] - A[0]; int continuous = 2; int slices = 0; for (int i = 2; i < A.length; i++) { int current = A[i]; int newDiff = current - prev; boolean isContinuous = newDiff == diff; if (isContinuous) { continuous++; } if (!isContinuous || i == A.length - 1) { slices += slicesOf(continuous); continuous = 2; } diff = newDiff; prev = current; } return slices; } }  }
Solution { public int numberOfArithmeticSlices(int[] A) { if (A.length < 3) { return 0; } else { int prev = A[1]; int diff = A[1] - A[0]; int continuous = 2; int slices = 0; for (int i = 2; i < A.length; i++) { int current = A[i]; int newDiff = current - prev; boolean isContinuous = newDiff == diff; if (isContinuous) { continuous++; } if (!isContinuous || i == A.length - 1) { slices += slicesOf(continuous); continuous = 2; } diff = newDiff; prev = current; } return slices; } }  int numberOfArithmeticSlices(int[] A); }
Solution { public int numberOfArithmeticSlices(int[] A) { if (A.length < 3) { return 0; } else { int prev = A[1]; int diff = A[1] - A[0]; int continuous = 2; int slices = 0; for (int i = 2; i < A.length; i++) { int current = A[i]; int newDiff = current - prev; boolean isContinuous = newDiff == diff; if (isContinuous) { continuous++; } if (!isContinuous || i == A.length - 1) { slices += slicesOf(continuous); continuous = 2; } diff = newDiff; prev = current; } return slices; } }  int numberOfArithmeticSlices(int[] A);  }