publicclassSolution { publicintCountTriplets(int[] arr) { int n = arr.Length; int[] s = newint[n + 1]; for (int i = 0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j; k < n; ++k) { if (s[i] == s[k + 1]) { ++ans; } } } } return ans; } }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funccountTriplets(arr []int) (ans int) { n := len(arr) s := make([]int, n+1) for i, val := range arr { s[i+1] = s[i] ^ val } for i := 0; i < n; i++ { for j := i + 1; j < n; j++ { for k := j; k < n; k++ { if s[i] == s[k+1] { ans++ } } } } return }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defcountTriplets(self, arr: List[int]) -> int: n = len(arr) s = [0] for val in arr: s.append(s[-1] ^ val) ans = 0 for i inrange(n): for j inrange(i + 1, n): for k inrange(j, n): if s[i] == s[k + 1]: ans += 1 return ans
var countTriplets = function(arr) { const n = arr.length; const s = [0]; for (const num of arr) { s.push(s[s.length - 1] ^ num); }
let ans = 0; for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { for (let k = j; k < n; k++) { if (s[i] === s[k + 1]) { ans++; } } } }
return ans; };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
intcountTriplets(int* arr, int arrSize) { int n = arrSize; int s[n + 1]; s[0] = 0; for (int i = 0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } int ans = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { for (int k = j; k < n; ++k) { if (s[i] == s[k + 1]) { ++ans; } } } } return ans; }
classSolution { public: intcountTriplets(vector<int> &arr){ int n = arr.size(); vector<int> s(n + 1); for (int i = 0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } int ans = 0; for (int i = 0; i < n; ++i) { for (int k = i + 1; k < n; ++k) { if (s[i] == s[k + 1]) { ans += k - i; } } } return ans; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicintcountTriplets(int[] arr) { intn= arr.length; int[] s = newint[n + 1]; for (inti=0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } intans=0; for (inti=0; i < n; ++i) { for (intk= i + 1; k < n; ++k) { if (s[i] == s[k + 1]) { ans += k - i; } } } return ans; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicclassSolution { publicintCountTriplets(int[] arr) { int n = arr.Length; int[] s = newint[n + 1]; for (int i = 0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } int ans = 0; for (int i = 0; i < n; ++i) { for (int k = i + 1; k < n; ++k) { if (s[i] == s[k + 1]) { ans += k - i; } } } return ans; } }
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funccountTriplets(arr []int) (ans int) { n := len(arr) s := make([]int, n+1) for i, val := range arr { s[i+1] = s[i] ^ val } for i := 0; i < n; i++ { for k := i + 1; k < n; k++ { if s[i] == s[k+1] { ans += k - i } } } return }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defcountTriplets(self, arr: List[int]) -> int: n = len(arr) s = [0] for val in arr: s.append(s[-1] ^ val) ans = 0 for i inrange(n): for k inrange(i + 1, n): if s[i] == s[k + 1]: ans += k - i return ans
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var countTriplets = function(arr) { const n = arr.length; const s = [0]; for (const num of arr) { s.push(s[s.length - 1] ^ num); }
let ans = 0; for (let i = 0; i < n; i++) { for (let k = i + 1; k < n; k++) { if (s[i] === s[k + 1]) { ans += k - i; } } }
return ans; };
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intcountTriplets(int* arr, int arrSize) { int n = arrSize; int s[n + 1]; s[0] = 0; for (int i = 0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } int ans = 0; for (int i = 0; i < n; ++i) { for (int k = i + 1; k < n; ++k) { if (s[i] == s[k + 1]) { ans += k - i; } } } return ans; }
publicclassSolution { publicintCountTriplets(int[] arr) { int n = arr.Length; int[] s = newint[n + 1]; for (int i = 0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } Dictionary<int, int> cnt = new Dictionary<int, int>(); Dictionary<int, int> total = new Dictionary<int, int>(); int ans = 0; for (int k = 0; k < n; ++k) { if (cnt.ContainsKey(s[k + 1])) { ans += cnt[s[k + 1]] * k - total[s[k + 1]]; } if (!cnt.ContainsKey(s[k])) { cnt.Add(s[k], 1); } else { ++cnt[s[k]]; } if (!total.ContainsKey(s[k])) { total.Add(s[k], k); } else { total[s[k]] += k; } } return ans; } }
[sol3-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funccountTriplets(arr []int) (ans int) { n := len(arr) s := make([]int, n+1) for i, v := range arr { s[i+1] = s[i] ^ v } cnt := map[int]int{} total := map[int]int{} for k := 0; k < n; k++ { if m, has := cnt[s[k+1]]; has { ans += m*k - total[s[k+1]] } cnt[s[k]]++ total[s[k]] += k } return }
[sol3-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defcountTriplets(self, arr: List[int]) -> int: n = len(arr) s = [0] for val in arr: s.append(s[-1] ^ val) cnt, total = Counter(), Counter() ans = 0 for k inrange(n): if s[k + 1] in cnt: ans += cnt[s[k + 1]] * k - total[s[k + 1]] cnt[s[k]] += 1 total[s[k]] += k
return ans
[sol3-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var countTriplets = function(arr) { const n = arr.length; s = [0]; for (const num of arr) { s.push(s[s.length - 1] ^ num); }
const cnt = newMap(), total = newMap(); let ans = 0; for (let k = 0; k < n; k++) { if (cnt.has(s[k + 1])) { ans += cnt.get(s[k + 1]) * k - total.get(s[k + 1]); } cnt.set(s[k], (cnt.get(s[k]) || 0) + 1); total.set(s[k], (total.get(s[k]) || 0) + k); }
intcountTriplets(int* arr, int arrSize) { int n = arrSize; int s[n + 1]; s[0] = 0; for (int i = 0; i < n; ++i) { s[i + 1] = s[i] ^ arr[i]; } structHashTable *cnt =NULL, *total = NULL; int ans = 0; for (int k = 0; k < n; ++k) { if (count(cnt, s[k + 1])) { ans += getValue(cnt, s[k + 1]) * k - getValue(total, s[k + 1]); } addValue(&cnt, s[k], 1); addValue(&total, s[k], k); } return ans; }
优化
我们可以在计算异或前缀和的同时计算答案,从而做到仅遍历 arr 一次就计算出答案。
[sol4-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { public: intcountTriplets(vector<int> &arr){ int n = arr.size(); unordered_map<int, int> cnt, total; int ans = 0, s = 0; for (int k = 0; k < n; ++k) { int val = arr[k]; if (cnt.count(s ^ val)) { ans += cnt[s ^ val] * k - total[s ^ val]; } ++cnt[s]; total[s] += k; s ^= val; } return ans; } };
[sol4-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution { publicintcountTriplets(int[] arr) { intn= arr.length; Map<Integer, Integer> cnt = newHashMap<Integer, Integer>(); Map<Integer, Integer> total = newHashMap<Integer, Integer>(); intans=0, s = 0; for (intk=0; k < n; ++k) { intval= arr[k]; if (cnt.containsKey(s ^ val)) { ans += cnt.get(s ^ val) * k - total.get(s ^ val); } cnt.put(s, cnt.getOrDefault(s, 0) + 1); total.put(s, total.getOrDefault(s, 0) + k); s ^= val; } return ans; } }
publicclassSolution { publicintCountTriplets(int[] arr) { int n = arr.Length; Dictionary<int, int> cnt = new Dictionary<int, int>(); Dictionary<int, int> total = new Dictionary<int, int>(); int ans = 0, s = 0; for (int k = 0; k < n; ++k) { int val = arr[k]; if (cnt.ContainsKey(s ^ val)) { ans += cnt[s ^ val] * k - total[s ^ val]; } if (!cnt.ContainsKey(s)) { cnt.Add(s, 1); } else { ++cnt[s]; } if (!total.ContainsKey(s)) { total.Add(s, k); } else { total[s] += k; } s ^= val; } return ans; } }
[sol4-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
funccountTriplets(arr []int) (ans int) { cnt := map[int]int{} total := map[int]int{} s := 0 for k, val := range arr { if m, has := cnt[s^val]; has { ans += m*k - total[s^val] } cnt[s]++ total[s] += k s ^= val } return }
[sol4-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution: defcountTriplets(self, arr: List[int]) -> int: cnt, total = Counter(), Counter() ans = s = 0
for k, val inenumerate(arr): if (t := s ^ val) in cnt: ans += cnt[t] * k - total[t] cnt[s] += 1 total[s] += k s = t
return ans
[sol4-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
var countTriplets = function(arr) { const cnt = newMap(), total = newMap(); let ans = 0, s = 0;
for (const [k, val] of arr.entries()) { const t = s ^ val; if (cnt.has(t)) { ans += cnt.get(t) * k - total.get(t); } cnt.set(s, (cnt.get(s) || 0) + 1); total.set(s, (total.get(s) || 0) + k); s = t; } return ans; };
intcountTriplets(int* arr, int arrSize) { int n = arrSize; structHashTable *cnt =NULL, *total = NULL; int ans = 0, s = 0; for (int k = 0; k < n; ++k) { int val = arr[k]; if (count(cnt, s ^ val)) { ans += getValue(cnt, s ^ val) * k - getValue(total, s ^ val); } addValue(&cnt, s, 1); addValue(&total, s, k); s ^= val; } return ans; }