Arithmetic Slices II - Subsequence LeetCode solution in Java
1class Solution {2public int numberOfArithmeticSlices(int[] nums) {3int n = nums.length;45HashMap<Integer, Integer>[] dp = new HashMap[n];67for (int i = 0; i < n; i++) {8dp[i] = new HashMap<Integer, Integer>();9}1011int ans = 0;1213for (int i = 1; i < n; i++) {14for (int j = 0; j < i; j++) {15long cd = (long) nums[i] - (long) nums[j];1617if (cd <= Integer.MIN_VALUE || cd >= Integer.MAX_VALUE) {18continue;19}2021int endingAtj = dp[j].getOrDefault((int) cd, 0);22int endingAti = dp[i].getOrDefault((int) cd, 0);2324ans += endingAtj;2526dp[i].put((int) cd, endingAtj + endingAti + 1);27}28}2930return ans;31}32}