1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| class Solution { int[] nums; int n; Boolean[] f;
public boolean validPartition(int[] nums) { this.nums = nums; n = nums.length; f = new Boolean[n]; return dfs(0); }
private boolean dfs(int i) { if (i == n) return true; if (f[i] != null) return f[i]; boolean a = i + 1 < n && nums[i] == nums[i + 1]; boolean b = i + 2 < n && nums[i] == nums[i + 1] && nums[i + 1] == nums[i + 2]; boolean c = i + 2 < n && nums[i + 1] - nums[i] == 1 && nums[i + 2] - nums[i + 1] == 1; return f[i] = ((a && dfs(i + 2)) || ((b || c) && dfs(i + 3))); } }
|