|
| 1 | +--- |
| 2 | +title: 1534.统计好三元组:系列题之-easy等级先模拟 |
| 3 | +date: 2025-04-15 13:27:51 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 枚举] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】1534.统计好三元组:系列题之-easy等级先模拟 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/count-good-triplets/](https://leetcode.cn/problems/count-good-triplets/) |
| 11 | + |
| 12 | +<p>给你一个整数数组 <code>arr</code> ,以及 <code>a</code>、<code>b</code> 、<code>c</code> 三个整数。请你统计其中好三元组的数量。</p> |
| 13 | + |
| 14 | +<p>如果三元组 <code>(arr[i], arr[j], arr[k])</code> 满足下列全部条件,则认为它是一个 <strong>好三元组</strong> 。</p> |
| 15 | + |
| 16 | +<ul> |
| 17 | + <li><code>0 <= i < j < k < arr.length</code></li> |
| 18 | + <li><code>|arr[i] - arr[j]| <= a</code></li> |
| 19 | + <li><code>|arr[j] - arr[k]| <= b</code></li> |
| 20 | + <li><code>|arr[i] - arr[k]| <= c</code></li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p>其中 <code>|x|</code> 表示 <code>x</code> 的绝对值。</p> |
| 24 | + |
| 25 | +<p>返回 <strong>好三元组的数量</strong> 。</p> |
| 26 | + |
| 27 | +<p> </p> |
| 28 | + |
| 29 | +<p><strong>示例 1:</strong></p> |
| 30 | + |
| 31 | +<pre><strong>输入:</strong>arr = [3,0,1,1,9,7], a = 7, b = 2, c = 3 |
| 32 | +<strong>输出:</strong>4 |
| 33 | +<strong>解释:</strong>一共有 4 个好三元组:[(3,0,1), (3,0,1), (3,1,1), (0,1,1)] 。 |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong>示例 2:</strong></p> |
| 37 | + |
| 38 | +<pre><strong>输入:</strong>arr = [1,1,2,2,3], a = 0, b = 0, c = 1 |
| 39 | +<strong>输出:</strong>0 |
| 40 | +<strong>解释:</strong>不存在满足所有条件的三元组。 |
| 41 | +</pre> |
| 42 | + |
| 43 | +<p> </p> |
| 44 | + |
| 45 | +<p><strong>提示:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>3 <= arr.length <= 100</code></li> |
| 49 | + <li><code>0 <= arr[i] <= 1000</code></li> |
| 50 | + <li><code>0 <= a, b, c <= 1000</code></li> |
| 51 | +</ul> |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +## 解题方法:三层循环 |
| 56 | + |
| 57 | +第一层用$i$从$0$到$n-1$,第二层用$j$从$i+1$到$n-1$,第三层用$k$从$j+1$到$n-1$,判断这个三元组是否为“好三元组”。 |
| 58 | + |
| 59 | ++ 时间复杂度$O(len(arr)^3)$ |
| 60 | ++ 空间复杂度$O(1)$ |
| 61 | + |
| 62 | +### AC代码 |
| 63 | + |
| 64 | +#### C++ |
| 65 | + |
| 66 | +```cpp |
| 67 | +/* |
| 68 | + * @Author: LetMeFly |
| 69 | + * @Date: 2025-04-15 10:24:13 |
| 70 | + * @LastEditors: LetMeFly.xyz |
| 71 | + * @LastEditTime: 2025-04-15 10:28:19 |
| 72 | + */ |
| 73 | +class Solution { |
| 74 | +public: |
| 75 | + int countGoodTriplets(vector<int>& arr, int a, int b, int c) { |
| 76 | + int ans = 0, n = arr.size(); |
| 77 | + for (int i = 0; i < n; i++) { |
| 78 | + for (int j = i + 1; j < n; j++) { |
| 79 | + if (abs(arr[i] - arr[j]) > a) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + for (int k = j + 1; k < n; k++) { |
| 83 | + ans += abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + return ans; |
| 88 | + } |
| 89 | +}; |
| 90 | +``` |
| 91 | +
|
| 92 | +#### Python |
| 93 | +
|
| 94 | +```python |
| 95 | +''' |
| 96 | +Author: LetMeFly |
| 97 | +Date: 2025-04-15 10:28:55 |
| 98 | +LastEditors: LetMeFly.xyz |
| 99 | +LastEditTime: 2025-04-15 10:34:14 |
| 100 | +''' |
| 101 | +from typing import List |
| 102 | +
|
| 103 | +class Solution: |
| 104 | + def countGoodTriplets(self, arr: List[int], a: int, b: int, c: int) -> int: |
| 105 | + # return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[j] - arr[k]) <= c for k in range(j + 1, len(arr)) for j in range(i + 1, len(arr)) for i in range(len(arr))) |
| 106 | + return sum(abs(arr[i] - arr[j]) <= a and abs(arr[j] - arr[k]) <= b and abs(arr[i] - arr[k]) <= c for i in range(len(arr)) for j in range(i + 1, len(arr)) for k in range(j + 1, len(arr))) |
| 107 | +``` |
| 108 | + |
| 109 | +#### Java |
| 110 | + |
| 111 | +```java |
| 112 | +/* |
| 113 | + * @Author: LetMeFly |
| 114 | + * @Date: 2025-04-15 10:35:43 |
| 115 | + * @LastEditors: LetMeFly.xyz |
| 116 | + * @LastEditTime: 2025-04-15 10:37:19 |
| 117 | + */ |
| 118 | +class Solution { |
| 119 | + public int countGoodTriplets(int[] arr, int a, int b, int c) { |
| 120 | + int ans = 0, n = arr.length; |
| 121 | + for (int i = 0; i < n; i++) { |
| 122 | + for (int j = i + 1; j < n; j++) { |
| 123 | + for (int k = j + 1; k < n; k++) { |
| 124 | + if (Math.abs(arr[i] - arr[j]) <= a && Math.abs(arr[j] - arr[k]) <= b && Math.abs(arr[i] - arr[k]) <= c) { |
| 125 | + ans++; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return ans; |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +#### Go |
| 136 | + |
| 137 | +```go |
| 138 | +/* |
| 139 | + * @Author: LetMeFly |
| 140 | + * @Date: 2025-04-15 10:39:01 |
| 141 | + * @LastEditors: LetMeFly.xyz |
| 142 | + * @LastEditTime: 2025-04-15 10:43:14 |
| 143 | + */ |
| 144 | +package main |
| 145 | + |
| 146 | +func abs1534(a int) int { |
| 147 | + if a < 0 { |
| 148 | + return -a |
| 149 | + } |
| 150 | + return a |
| 151 | +} |
| 152 | + |
| 153 | +func countGoodTriplets(arr []int, a int, b int, c int) (ans int) { |
| 154 | + for i := range arr { |
| 155 | + for j := i + 1; j < len(arr); j++ { |
| 156 | + for k := j + 1; k < len(arr); k++ { |
| 157 | + if abs1534(arr[i] - arr[j]) <= a && abs1534(arr[j] - arr[k]) <= b && abs1534(arr[i] - arr[k]) <= c { |
| 158 | + ans++ |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + return |
| 164 | +} |
| 165 | +``` |
| 166 | + |
| 167 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/147249275)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/04/15/LeetCode%201534.%E7%BB%9F%E8%AE%A1%E5%A5%BD%E4%B8%89%E5%85%83%E7%BB%84/)哦~ |
| 168 | +> |
| 169 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments