Skip to content

Commit c80c82f

Browse files
committed
feat: add solutions to lc problem: No.1534
No.1534.Count Good Triplets
1 parent 035a59c commit c80c82f

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed

solution/1500-1599/1534.Count Good Triplets/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,52 @@ function countGoodTriplets(arr: number[], a: number, b: number, c: number): numb
183183
}
184184
```
185185

186+
#### Rust
187+
188+
```rust
189+
impl Solution {
190+
pub fn count_good_triplets(arr: Vec<i32>, a: i32, b: i32, c: i32) -> i32 {
191+
let n = arr.len();
192+
let mut ans = 0;
193+
194+
for i in 0..n {
195+
for j in i + 1..n {
196+
for k in j + 1..n {
197+
if (arr[i] - arr[j]).abs() <= a && (arr[j] - arr[k]).abs() <= b && (arr[i] - arr[k]).abs() <= c {
198+
ans += 1;
199+
}
200+
}
201+
}
202+
}
203+
204+
ans
205+
}
206+
}
207+
```
208+
209+
#### C#
210+
211+
```cs
212+
public class Solution {
213+
public int CountGoodTriplets(int[] arr, int a, int b, int c) {
214+
int n = arr.Length;
215+
int ans = 0;
216+
217+
for (int i = 0; i < n; ++i) {
218+
for (int j = i + 1; j < n; ++j) {
219+
for (int k = j + 1; k < n; ++k) {
220+
if (Math.Abs(arr[i] - arr[j]) <= a && Math.Abs(arr[j] - arr[k]) <= b && Math.Abs(arr[i] - arr[k]) <= c) {
221+
++ans;
222+
}
223+
}
224+
}
225+
}
226+
227+
return ans;
228+
}
229+
}
230+
```
231+
186232
<!-- tabs:end -->
187233

188234
<!-- solution:end -->

solution/1500-1599/1534.Count Good Triplets/README_EN.md

+46
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,52 @@ function countGoodTriplets(arr: number[], a: number, b: number, c: number): numb
183183
}
184184
```
185185

186+
#### Rust
187+
188+
```rust
189+
impl Solution {
190+
pub fn count_good_triplets(arr: Vec<i32>, a: i32, b: i32, c: i32) -> i32 {
191+
let n = arr.len();
192+
let mut ans = 0;
193+
194+
for i in 0..n {
195+
for j in i + 1..n {
196+
for k in j + 1..n {
197+
if (arr[i] - arr[j]).abs() <= a && (arr[j] - arr[k]).abs() <= b && (arr[i] - arr[k]).abs() <= c {
198+
ans += 1;
199+
}
200+
}
201+
}
202+
}
203+
204+
ans
205+
}
206+
}
207+
```
208+
209+
#### C#
210+
211+
```cs
212+
public class Solution {
213+
public int CountGoodTriplets(int[] arr, int a, int b, int c) {
214+
int n = arr.Length;
215+
int ans = 0;
216+
217+
for (int i = 0; i < n; ++i) {
218+
for (int j = i + 1; j < n; ++j) {
219+
for (int k = j + 1; k < n; ++k) {
220+
if (Math.Abs(arr[i] - arr[j]) <= a && Math.Abs(arr[j] - arr[k]) <= b && Math.Abs(arr[i] - arr[k]) <= c) {
221+
++ans;
222+
}
223+
}
224+
}
225+
}
226+
227+
return ans;
228+
}
229+
}
230+
```
231+
186232
<!-- tabs:end -->
187233

188234
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
public class Solution {
2+
public int CountGoodTriplets(int[] arr, int a, int b, int c) {
3+
int n = arr.Length;
4+
int ans = 0;
5+
6+
for (int i = 0; i < n; ++i) {
7+
for (int j = i + 1; j < n; ++j) {
8+
for (int k = j + 1; k < n; ++k) {
9+
if (Math.Abs(arr[i] - arr[j]) <= a && Math.Abs(arr[j] - arr[k]) <= b && Math.Abs(arr[i] - arr[k]) <= c) {
10+
++ans;
11+
}
12+
}
13+
}
14+
}
15+
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
impl Solution {
2+
pub fn count_good_triplets(arr: Vec<i32>, a: i32, b: i32, c: i32) -> i32 {
3+
let n = arr.len();
4+
let mut ans = 0;
5+
6+
for i in 0..n {
7+
for j in i + 1..n {
8+
for k in j + 1..n {
9+
if (arr[i] - arr[j]).abs() <= a
10+
&& (arr[j] - arr[k]).abs() <= b
11+
&& (arr[i] - arr[k]).abs() <= c
12+
{
13+
ans += 1;
14+
}
15+
}
16+
}
17+
}
18+
19+
ans
20+
}
21+
}

0 commit comments

Comments
 (0)