classSolution { public: intcountTriplets(vector<int>& nums){ vector<int> cnt(1 << 16); for (int x: nums) { for (int y: nums) { ++cnt[x & y]; } } int ans = 0; for (int x: nums) { for (int mask = 0; mask < (1 << 16); ++mask) { if ((x & mask) == 0) { ans += cnt[mask]; } } } return ans; } };
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicintcountTriplets(int[] nums) { int[] cnt = newint[1 << 16]; for (int x : nums) { for (int y : nums) { ++cnt[x & y]; } } intans=0; for (int x : nums) { for (intmask=0; mask < (1 << 16); ++mask) { if ((x & mask) == 0) { ans += cnt[mask]; } } } return ans; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintCountTriplets(int[] nums) { int[] cnt = newint[1 << 16]; foreach (int x in nums) { foreach (int y in nums) { ++cnt[x & y]; } } int ans = 0; foreach (int x in nums) { for (int mask = 0; mask < (1 << 16); ++mask) { if ((x & mask) == 0) { ans += cnt[mask]; } } } return ans; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10
classSolution: defcountTriplets(self, nums: List[int]) -> int: cnt = Counter((x & y) for x in nums for y in nums) ans = 0 for x in nums: for mask, freq in cnt.items(): if (x & mask) == 0: ans += freq return ans
intcountTriplets(int* nums, int numsSize) { int *cnt = (int *)calloc(sizeof(int), 1 << 16); for (int i = 0; i < numsSize; i++) { int x = nums[i]; for (int j = 0; j < numsSize; j++) { int y = nums[j]; ++cnt[x & y]; } } int ans = 0; for (int i = 0; i < numsSize; i++) { int x = nums[i]; for (int mask = 0; mask < (1 << 16); ++mask) { if ((x & mask) == 0) { ans += cnt[mask]; } } } free(cnt); return ans; }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var countTriplets = function(nums) { const cnt = newArray(1 << 16).fill(0); for (const x of nums) { for (const y of nums) { ++cnt[x & y]; } } let ans = 0; for (const x of nums) { for (let mask = 0; mask < (1 << 16); ++mask) { if ((x & mask) === 0) { ans += cnt[mask]; } } } return ans; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funccountTriplets(nums []int)int { cnt := make(map[int]int) for i := range nums { for j := range nums { cnt[nums[i]&nums[j]]++ } } res := 0 for i := range nums { for k, v := range cnt { if k&nums[i] == 0 { res += v } } } return res }
复杂度分析
时间复杂度:O(n^2 + C \cdot n),其中 n 是数组 nums 的长度,C 是数组 nums 中的元素范围,在本题中 C = 2^{16。
classSolution { public: intcountTriplets(vector<int>& nums){ vector<int> cnt(1 << 16); for (int x: nums) { for (int y: nums) { ++cnt[x & y]; } } int ans = 0; for (int x: nums) { x = x ^ 0xffff; for (int sub = x; sub; sub = (sub - 1) & x) { ans += cnt[sub]; } ans += cnt[0]; } return ans; } };
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { publicintcountTriplets(int[] nums) { int[] cnt = newint[1 << 16]; for (int x : nums) { for (int y : nums) { ++cnt[x & y]; } } intans=0; for (int x : nums) { x = x ^ 0xffff; for (intsub= x; sub != 0; sub = (sub - 1) & x) { ans += cnt[sub]; } ans += cnt[0]; } return ans; } }
[sol2-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintCountTriplets(int[] nums) { int[] cnt = newint[1 << 16]; foreach (int x in nums) { foreach (int y in nums) { ++cnt[x & y]; } } int ans = 0; foreach (int x in nums) { int y = x ^ 0xffff; for (int sub = y; sub != 0; sub = (sub - 1) & y) { ans += cnt[sub]; } ans += cnt[0]; } return ans; } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution: defcountTriplets(self, nums: List[int]) -> int: cnt = Counter((x & y) for x in nums for y in nums) ans = 0 for x in nums: sub = x = x ^ 0xffff whileTrue: if sub in cnt: ans += cnt[sub] if sub == 0: break sub = (sub - 1) & x return ans
intcountTriplets(int* nums, int numsSize) { int *cnt = (int *)calloc(sizeof(int), 1 << 16); for (int i = 0; i < numsSize; i++) { int x = nums[i]; for (int j = 0; j < numsSize; j++) { int y = nums[j]; ++cnt[x & y]; } } int ans = 0; for (int i = 0; i < numsSize; i++) { int x = nums[i] ^ 0xffff; for (int sub = x; sub; sub = (sub - 1) & x) { ans += cnt[sub]; } ans += cnt[0]; } free(cnt); return ans; }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
var countTriplets = function(nums) { const cnt = newArray(1 << 16).fill(0); for (const x of nums) { for (const y of nums) { ++cnt[x & y]; } } let ans = 0; for (let x of nums) { x = x ^ 0xffff; for (let sub = x; sub !== 0; sub = (sub - 1) & x) { ans += cnt[sub]; } ans += cnt[0]; } return ans; };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funccountTriplets(nums []int)int { var cnt [1 << 16]int for i := range nums { for j := range nums { cnt[nums[i]&nums[j]]++ } } res := 0 for i := range nums { x := nums[i] ^ 0xffff for sub := x; sub > 0; sub = (sub - 1) & x { res += cnt[sub] } res += cnt[0] } return res }
复杂度分析
时间复杂度:O(n^2 + C \cdot n),其中 n 是数组 nums 的长度,C 是数组 nums 中的元素范围,在本题中 C = 2^{16。