Skip to content

Commit df3cc36

Browse files
Merge branch 'doocs:main' into main
2 parents 09668b7 + 0d0ad7c commit df3cc36

File tree

124 files changed

+34531
-28335
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+34531
-28335
lines changed

images/starcharts.svg

Lines changed: 28184 additions & 28077 deletions
Loading

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

Lines changed: 46 additions & 0 deletions
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

Lines changed: 46 additions & 0 deletions
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 -->
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 21 additions & 0 deletions
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+
}

solution/1900-1999/1922.Count Good Numbers/README.md

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,15 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:快速幂
71+
72+
长度为 $n$ 的好数字,偶数下标一共有 $\lceil \frac{n}{2} \rceil = \lfloor \frac{n + 1}{2} \rfloor$ 位,偶数下标可以填入 $5$ 种数字($0, 2, 4, 6, 8$);奇数下标一共有 $\lfloor \frac{n}{2} \rfloor$ 位,奇数下标可以填入 $4$ 种数字($2, 3, 5, 7$)。因此长度为 $n$ 的好数字的个数为:
73+
74+
$$
75+
ans = 5^{\lceil \frac{n}{2} \rceil} \times 4^{\lfloor \frac{n}{2} \rfloor}
76+
$$
77+
78+
我们可以使用快速幂来计算 $5^{\lceil \frac{n}{2} \rceil}$ 和 $4^{\lfloor \frac{n}{2} \rfloor}$,时间复杂度为 $O(\log n)$,空间复杂度为 $O(1)$。
7179

7280
<!-- tabs:start -->
7381

@@ -84,13 +92,13 @@ class Solution:
8492

8593
```java
8694
class Solution {
87-
private int mod = 1000000007;
95+
private final int mod = (int) 1e9 + 7;
8896

8997
public int countGoodNumbers(long n) {
90-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % mod);
98+
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
9199
}
92100

93-
private long myPow(long x, long n) {
101+
private long qpow(long x, long n) {
94102
long res = 1;
95103
while (n != 0) {
96104
if ((n & 1) == 1) {
@@ -107,25 +115,22 @@ class Solution {
107115
#### C++
108116

109117
```cpp
110-
int MOD = 1000000007;
111-
112118
class Solution {
113119
public:
114120
int countGoodNumbers(long long n) {
115-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD);
116-
}
117-
118-
private:
119-
long long myPow(long long x, long long n) {
120-
long long res = 1;
121-
while (n) {
122-
if ((n & 1) == 1) {
123-
res = res * x % MOD;
121+
const int mod = 1e9 + 7;
122+
auto qpow = [](long long x, long long n) -> long long {
123+
long long res = 1;
124+
while (n) {
125+
if ((n & 1) == 1) {
126+
res = res * x % mod;
127+
}
128+
x = x * x % mod;
129+
n >>= 1;
124130
}
125-
x = x * x % MOD;
126-
n >>= 1;
127-
}
128-
return res;
131+
return res;
132+
};
133+
return qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod;
129134
}
130135
};
131136
```
@@ -152,6 +157,28 @@ func myPow(x, n int64) int64 {
152157
}
153158
```
154159

160+
#### TypeScript
161+
162+
```ts
163+
function countGoodNumbers(n: number): number {
164+
const mod = 1000000007n;
165+
const qpow = (x: bigint, n: bigint): bigint => {
166+
let res = 1n;
167+
while (n > 0n) {
168+
if (n & 1n) {
169+
res = (res * x) % mod;
170+
}
171+
x = (x * x) % mod;
172+
n >>= 1n;
173+
}
174+
return res;
175+
};
176+
const a = qpow(5n, BigInt(n + 1) / 2n);
177+
const b = qpow(4n, BigInt(n) / 2n);
178+
return Number((a * b) % mod);
179+
}
180+
```
181+
155182
<!-- tabs:end -->
156183

157184
<!-- solution:end -->

solution/1900-1999/1922.Count Good Numbers/README_EN.md

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Fast Exponentiation
69+
70+
For a "good number" of length $n$, the even-indexed positions have $\lceil \frac{n}{2} \rceil = \lfloor \frac{n + 1}{2} \rfloor$ digits, and these positions can be filled with $5$ different digits ($0, 2, 4, 6, 8$). The odd-indexed positions have $\lfloor \frac{n}{2} \rfloor$ digits, and these positions can be filled with $4$ different digits ($2, 3, 5, 7$). Therefore, the total number of "good numbers" of length $n$ is:
71+
72+
$$
73+
ans = 5^{\lceil \frac{n}{2} \rceil} \times 4^{\lfloor \frac{n}{2} \rfloor}
74+
$$
75+
76+
We can use fast exponentiation to compute $5^{\lceil \frac{n}{2} \rceil}$ and $4^{\lfloor \frac{n}{2} \rfloor}$. The time complexity is $O(\log n)$, and the space complexity is $O(1)$.
6977

7078
<!-- tabs:start -->
7179

@@ -82,13 +90,13 @@ class Solution:
8290

8391
```java
8492
class Solution {
85-
private int mod = 1000000007;
93+
private final int mod = (int) 1e9 + 7;
8694

8795
public int countGoodNumbers(long n) {
88-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % mod);
96+
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
8997
}
9098

91-
private long myPow(long x, long n) {
99+
private long qpow(long x, long n) {
92100
long res = 1;
93101
while (n != 0) {
94102
if ((n & 1) == 1) {
@@ -105,25 +113,22 @@ class Solution {
105113
#### C++
106114

107115
```cpp
108-
int MOD = 1000000007;
109-
110116
class Solution {
111117
public:
112118
int countGoodNumbers(long long n) {
113-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD);
114-
}
115-
116-
private:
117-
long long myPow(long long x, long long n) {
118-
long long res = 1;
119-
while (n) {
120-
if ((n & 1) == 1) {
121-
res = res * x % MOD;
119+
const int mod = 1e9 + 7;
120+
auto qpow = [](long long x, long long n) -> long long {
121+
long long res = 1;
122+
while (n) {
123+
if ((n & 1) == 1) {
124+
res = res * x % mod;
125+
}
126+
x = x * x % mod;
127+
n >>= 1;
122128
}
123-
x = x * x % MOD;
124-
n >>= 1;
125-
}
126-
return res;
129+
return res;
130+
};
131+
return qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod;
127132
}
128133
};
129134
```
@@ -150,6 +155,28 @@ func myPow(x, n int64) int64 {
150155
}
151156
```
152157

158+
#### TypeScript
159+
160+
```ts
161+
function countGoodNumbers(n: number): number {
162+
const mod = 1000000007n;
163+
const qpow = (x: bigint, n: bigint): bigint => {
164+
let res = 1n;
165+
while (n > 0n) {
166+
if (n & 1n) {
167+
res = (res * x) % mod;
168+
}
169+
x = (x * x) % mod;
170+
n >>= 1n;
171+
}
172+
return res;
173+
};
174+
const a = qpow(5n, BigInt(n + 1) / 2n);
175+
const b = qpow(4n, BigInt(n) / 2n);
176+
return Number((a * b) % mod);
177+
}
178+
```
179+
153180
<!-- tabs:end -->
154181

155182
<!-- solution:end -->
Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
int MOD = 1000000007;
2-
31
class Solution {
42
public:
53
int countGoodNumbers(long long n) {
6-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD);
7-
}
8-
9-
private:
10-
long long myPow(long long x, long long n) {
11-
long long res = 1;
12-
while (n) {
13-
if ((n & 1) == 1) {
14-
res = res * x % MOD;
4+
const int mod = 1e9 + 7;
5+
auto qpow = [](long long x, long long n) -> long long {
6+
long long res = 1;
7+
while (n) {
8+
if ((n & 1) == 1) {
9+
res = res * x % mod;
10+
}
11+
x = x * x % mod;
12+
n >>= 1;
1513
}
16-
x = x * x % MOD;
17-
n >>= 1;
18-
}
19-
return res;
14+
return res;
15+
};
16+
return qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod;
2017
}
2118
};

solution/1900-1999/1922.Count Good Numbers/Solution.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
2-
private int mod = 1000000007;
2+
private final int mod = (int) 1e9 + 7;
33

44
public int countGoodNumbers(long n) {
5-
return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % mod);
5+
return (int) (qpow(5, (n + 1) >> 1) * qpow(4, n >> 1) % mod);
66
}
77

8-
private long myPow(long x, long n) {
8+
private long qpow(long x, long n) {
99
long res = 1;
1010
while (n != 0) {
1111
if ((n & 1) == 1) {

0 commit comments

Comments
 (0)