记数组 nums 的大小为 n,使用三重循环,枚举所有 0 \le i \lt j \lt k \lt n 的三元组,如果三元组 (i, j, k) 满足 nums}[i] \ne \textit{nums}[j]、nums}[i] \ne \textit{nums}[k] 且 nums}[j] \ne \textit{nums}[k],那么将结果加 1,枚举结束后返回最终结果。
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: intunequalTriplets(vector<int>& nums){ int res = 0, n = nums.size(); for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { for (int k = j + 1; k < n; k++) { if (nums[i] != nums[j] && nums[i] != nums[k] && nums[j] != nums[k]) { res++; } } } } return res; } };
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13
funcunequalTriplets(nums []int)int { res, n := 0, len(nums) for i := 0; i < n; i++ { for j := i + 1; j < n; j++ { for k := j + 1; k < n; k++ { if nums[i] != nums[j] && nums[i] != nums[k] && nums[j] != nums[k] { res++ } } } } return res }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13
intunequalTriplets(int* nums, int numsSize) { int res = 0; for (int i = 0; i < numsSize; i++) { for (int j = i + 1; j < numsSize; j++) { for (int k = j + 1; k < numsSize; k++) { if (nums[i] != nums[j] && nums[i] != nums[k] && nums[j] != nums[k]) { res++; } } } } return res; }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10
classSolution: defunequalTriplets(self, nums: List[int]) -> int: res = 0 n = len(nums) for i inrange(n): for j inrange(i + 1, n): for k inrange(j + 1, n): if nums[i] != nums[j] and nums[i] != nums[k] and nums[j] != nums[k]: res += 1 return res
[sol1-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { publicintunequalTriplets(int[] nums) { intres=0, n = nums.length; for (inti=0; i < n; i++) { for (intj= i + 1; j < n; j++) { for (intk= j + 1; k < n; k++) { if (nums[i] != nums[j] && nums[i] != nums[k] && nums[j] != nums[k]) { res++; } } } } return res; } }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var unequalTriplets = function(nums) { let res = 0, n = nums.length; for (let i = 0; i < n; i++) { for (let j = i + 1; j < n; j++) { for (let k = j + 1; k < n; k++) { if (nums[i] != nums[j] && nums[i] != nums[k] && nums[j] != nums[k]) { res++; } } } } return res; };
复杂度分析
时间复杂度:O(n^3),其中 n 是数组 nums 的大小。
空间复杂度:O(1)。
方法二:排序
由题意可知,数组元素的相对顺序不影响结果,因此我们可以将数组 nums 从小到大进行排序。排序后,数组中的相同元素一定是相邻的。当我们以某一堆相同的数 [i, j) 作为三元组的中间元素时,这堆相同的元素的左边元素数目为 i,右边元素数目为 n - j,那么符合条件的三元组数目为:
i \times (j - i) \times (n - j)
对以上结果求和并返回最终结果。
[sol2-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { public: intunequalTriplets(vector<int>& nums){ sort(nums.begin(), nums.end()); int res = 0, n = nums.size(); for (int i = 0, j = 0; i < n; i = j) { while (j < n && nums[j] == nums[i]) { j++; } res += i * (j - i) * (n - j); } return res; } };
[sol2-Golang]
1 2 3 4 5 6 7 8 9 10 11
funcunequalTriplets(nums []int)int { sort.Ints(nums) res, n := 0, len(nums) for i, j := 0, 0; i < n; i = j { for j < n && nums[j] == nums[i] { j++ } res += i * (j - i) * (n - j) } return res }
intunequalTriplets(int* nums, int numsSize) { qsort(nums, numsSize, sizeof(int), cmp); int res = 0, n = numsSize; for (int i = 0, j = 0; i < n; i = j) { while (j < n && nums[j] == nums[i]) { j++; } res += i * (j - i) * (n - j); } return res; }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12
classSolution: defunequalTriplets(self, nums: List[int]) -> int: nums.sort() res = 0 n = len(nums) i = j = 0 while i < n: while j < n and nums[j] == nums[i]: j += 1 res += i * (j - i) * (n - j) i = j return res
[sol2-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13
classSolution { publicintunequalTriplets(int[] nums) { Arrays.sort(nums); intres=0, n = nums.length; for (inti=0, j = 0; i < n; i = j) { while (j < n && nums[j] == nums[i]) { j++; } res += i * (j - i) * (n - j); } return res; } }
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11
var unequalTriplets = function(nums) { nums.sort(); let res = 0, n = nums.length; for (let i = 0, j = 0; i < n; i = j) { while (j < n && nums[j] == nums[i]) { j++; } res += i * (j - i) * (n - j); } return res; };
classSolution { public: intunequalTriplets(vector<int>& nums){ unordered_map<int, int> count; for (auto x : nums) { count[x]++; } int res = 0, n = nums.size(), t = 0; for (auto [_, v] : count) { res += t * v * (n - t - v); t += v; } return res; } };
[sol3-Golang]
1 2 3 4 5 6 7 8 9 10 11
funcunequalTriplets(nums []int)int { count := map[int]int{} for _, x := range nums { count[x]++ } res, n, t := 0, len(nums), 0 for _, v := range count { res, t = res + t * v * (n - t - v), t + v } return res }
[sol3-C]
1 2 3 4 5 6 7 8 9 10 11 12 13
intunequalTriplets(int* nums, int numsSize) { int count[1001] = {0}; for (int i = 0; i < numsSize; i++) { count[nums[i]]++; } int res = 0, n = numsSize, t = 0; for (int i = 0; i <= 1000; i++) { int v = count[i]; res += t * v * (n - t - v); t += v; } return res; }
[sol3-Python3]
1 2 3 4 5 6 7 8 9 10
classSolution: defunequalTriplets(self, nums: List[int]) -> int: count = Counter(nums) res = 0 n = len(nums) t = 0 for _, v in count.items(): res += t * v * (n - t - v) t += v return res
[sol3-Java]
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution { publicintunequalTriplets(int[] nums) { Map<Integer, Integer> count = newHashMap<>(); for (int x : nums) { count.merge(x, 1, Integer::sum); } intres=0, n = nums.length, t = 0; for (Map.Entry<Integer, Integer> entry : count.entrySet()) { res += t * entry.getValue() * (n - t - entry.getValue()); t += entry.getValue(); } return res; } }
[sol3-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12
var unequalTriplets = function(nums) { let count = {}, res = 0, n = nums.length, t = 0; for (let x of nums) { count[x] = count[x] || 0; count[x]++; } for (let k in count) { res += t * count[k] * (n - t - count[k]); t += count[k]; } return res; };