1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| class Solution { List<Integer> list = new ArrayList<>(); List<List<Integer>> result = new ArrayList<>(); public int countGoodTriplets(int[] arr, int a, int b, int c) { dfs(0, arr, a, b, c); return result.size(); }
public void dfs(int cur, int[] arr, int a, int b, int c){ if (list.size() == 3){ int i = list.get(0); int j = list.get(1); int k = list.get(2); if (Math.abs(i - j) <= a && Math.abs(j - k) <= b && Math.abs(i - k) <= c) { result.add(new ArrayList(list)); } return; }
for (int i = cur; i < arr.length; i++) { list.add(arr[i]); dfs(i + 1, arr, a, b, c); list.remove(list.size() - 1); } } }
|