Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8bae47f

Browse files
authored
feat: update solutions to lc problem: No.2176 (doocs#3526)
1 parent 8221bdb commit 8bae47f

File tree

10 files changed

+94
-118
lines changed

10 files changed

+94
-118
lines changed

solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README.md

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### 方法一:暴力枚举
59+
### 方法一:枚举
60+
61+
我们先在 $[0, n)$ 的范围内枚举下标 $j$,然后在 $[0, j)$ 的范围内枚举下标 $i$,统计满足 $\textit{nums}[i] = \textit{nums}[j]$ 且 $(i \times j) \bmod k = 0$ 的数对个数。
62+
63+
时间复杂度 $O(n^2)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
6064

6165
<!-- tabs:start -->
6266

@@ -65,26 +69,22 @@ tags:
6569
```python
6670
class Solution:
6771
def countPairs(self, nums: List[int], k: int) -> int:
68-
n = len(nums)
69-
return sum(
70-
nums[i] == nums[j] and (i * j) % k == 0
71-
for i in range(n)
72-
for j in range(i + 1, n)
73-
)
72+
ans = 0
73+
for j, y in enumerate(nums):
74+
for i, x in enumerate(nums[:j]):
75+
ans += int(x == y and i * j % k == 0)
76+
return ans
7477
```
7578

7679
#### Java
7780

7881
```java
7982
class Solution {
8083
public int countPairs(int[] nums, int k) {
81-
int n = nums.length;
8284
int ans = 0;
83-
for (int i = 0; i < n; ++i) {
84-
for (int j = i + 1; j < n; ++j) {
85-
if (nums[i] == nums[j] && (i * j) % k == 0) {
86-
++ans;
87-
}
85+
for (int j = 1; j < nums.length; ++j) {
86+
for (int i = 0; i < j; ++i) {
87+
ans += nums[i] == nums[j] && (i * j % k) == 0 ? 1 : 0;
8888
}
8989
}
9090
return ans;
@@ -98,11 +98,10 @@ class Solution {
9898
class Solution {
9999
public:
100100
int countPairs(vector<int>& nums, int k) {
101-
int n = nums.size();
102101
int ans = 0;
103-
for (int i = 0; i < n; ++i) {
104-
for (int j = i + 1; j < n; ++j) {
105-
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
102+
for (int j = 1; j < nums.size(); ++j) {
103+
for (int i = 0; i < j; ++i) {
104+
ans += nums[i] == nums[j] && (i * j % k) == 0;
106105
}
107106
}
108107
return ans;
@@ -113,30 +112,27 @@ public:
113112
#### Go
114113
115114
```go
116-
func countPairs(nums []int, k int) int {
117-
n := len(nums)
118-
ans := 0
119-
for i, v := range nums {
120-
for j := i + 1; j < n; j++ {
121-
if v == nums[j] && (i*j)%k == 0 {
115+
func countPairs(nums []int, k int) (ans int) {
116+
for j, y := range nums {
117+
for i, x := range nums[:j] {
118+
if x == y && (i*j%k) == 0 {
122119
ans++
123120
}
124121
}
125122
}
126-
return ans
123+
return
127124
}
128125
```
129126

130127
#### TypeScript
131128

132129
```ts
133130
function countPairs(nums: number[], k: number): number {
134-
const n = nums.length;
135131
let ans = 0;
136-
for (let i = 0; i < n - 1; i++) {
137-
for (let j = i + 1; j < n; j++) {
132+
for (let j = 1; j < nums.length; ++j) {
133+
for (let i = 0; i < j; ++i) {
138134
if (nums[i] === nums[j] && (i * j) % k === 0) {
139-
ans++;
135+
++ans;
140136
}
141137
}
142138
}
@@ -149,12 +145,10 @@ function countPairs(nums: number[], k: number): number {
149145
```rust
150146
impl Solution {
151147
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
152-
let k = k as usize;
153-
let n = nums.len();
154148
let mut ans = 0;
155-
for i in 0..n - 1 {
156-
for j in i + 1..n {
157-
if nums[i] == nums[j] && (i * j) % k == 0 {
149+
for j in 1..nums.len() {
150+
for (i, &x) in nums[..j].iter().enumerate() {
151+
if x == nums[j] && (i * j) as i32 % k == 0 {
158152
ans += 1;
159153
}
160154
}
@@ -169,11 +163,9 @@ impl Solution {
169163
```c
170164
int countPairs(int* nums, int numsSize, int k) {
171165
int ans = 0;
172-
for (int i = 0; i < numsSize - 1; i++) {
173-
for (int j = i + 1; j < numsSize; j++) {
174-
if (nums[i] == nums[j] && i * j % k == 0) {
175-
ans++;
176-
}
166+
for (int j = 1; j < numsSize; ++j) {
167+
for (int i = 0; i < j; ++i) {
168+
ans += (nums[i] == nums[j] && (i * j % k) == 0);
177169
}
178170
}
179171
return ans;

solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/README_EN.md

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,11 @@ There are 4 pairs that meet all the requirements:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Enumeration
60+
61+
We first enumerate the index $j$ in the range $[0, n)$, and then enumerate the index $i$ in the range $[0, j)$. We count the number of pairs that satisfy $\textit{nums}[i] = \textit{nums}[j]$ and $(i \times j) \bmod k = 0$.
62+
63+
The time complexity is $O(n^2)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.
6064

6165
<!-- tabs:start -->
6266

@@ -65,26 +69,22 @@ There are 4 pairs that meet all the requirements:
6569
```python
6670
class Solution:
6771
def countPairs(self, nums: List[int], k: int) -> int:
68-
n = len(nums)
69-
return sum(
70-
nums[i] == nums[j] and (i * j) % k == 0
71-
for i in range(n)
72-
for j in range(i + 1, n)
73-
)
72+
ans = 0
73+
for j, y in enumerate(nums):
74+
for i, x in enumerate(nums[:j]):
75+
ans += int(x == y and i * j % k == 0)
76+
return ans
7477
```
7578

7679
#### Java
7780

7881
```java
7982
class Solution {
8083
public int countPairs(int[] nums, int k) {
81-
int n = nums.length;
8284
int ans = 0;
83-
for (int i = 0; i < n; ++i) {
84-
for (int j = i + 1; j < n; ++j) {
85-
if (nums[i] == nums[j] && (i * j) % k == 0) {
86-
++ans;
87-
}
85+
for (int j = 1; j < nums.length; ++j) {
86+
for (int i = 0; i < j; ++i) {
87+
ans += nums[i] == nums[j] && (i * j % k) == 0 ? 1 : 0;
8888
}
8989
}
9090
return ans;
@@ -98,11 +98,10 @@ class Solution {
9898
class Solution {
9999
public:
100100
int countPairs(vector<int>& nums, int k) {
101-
int n = nums.size();
102101
int ans = 0;
103-
for (int i = 0; i < n; ++i) {
104-
for (int j = i + 1; j < n; ++j) {
105-
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
102+
for (int j = 1; j < nums.size(); ++j) {
103+
for (int i = 0; i < j; ++i) {
104+
ans += nums[i] == nums[j] && (i * j % k) == 0;
106105
}
107106
}
108107
return ans;
@@ -113,30 +112,27 @@ public:
113112
#### Go
114113
115114
```go
116-
func countPairs(nums []int, k int) int {
117-
n := len(nums)
118-
ans := 0
119-
for i, v := range nums {
120-
for j := i + 1; j < n; j++ {
121-
if v == nums[j] && (i*j)%k == 0 {
115+
func countPairs(nums []int, k int) (ans int) {
116+
for j, y := range nums {
117+
for i, x := range nums[:j] {
118+
if x == y && (i*j%k) == 0 {
122119
ans++
123120
}
124121
}
125122
}
126-
return ans
123+
return
127124
}
128125
```
129126

130127
#### TypeScript
131128

132129
```ts
133130
function countPairs(nums: number[], k: number): number {
134-
const n = nums.length;
135131
let ans = 0;
136-
for (let i = 0; i < n - 1; i++) {
137-
for (let j = i + 1; j < n; j++) {
132+
for (let j = 1; j < nums.length; ++j) {
133+
for (let i = 0; i < j; ++i) {
138134
if (nums[i] === nums[j] && (i * j) % k === 0) {
139-
ans++;
135+
++ans;
140136
}
141137
}
142138
}
@@ -149,12 +145,10 @@ function countPairs(nums: number[], k: number): number {
149145
```rust
150146
impl Solution {
151147
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
152-
let k = k as usize;
153-
let n = nums.len();
154148
let mut ans = 0;
155-
for i in 0..n - 1 {
156-
for j in i + 1..n {
157-
if nums[i] == nums[j] && (i * j) % k == 0 {
149+
for j in 1..nums.len() {
150+
for (i, &x) in nums[..j].iter().enumerate() {
151+
if x == nums[j] && (i * j) as i32 % k == 0 {
158152
ans += 1;
159153
}
160154
}
@@ -169,11 +163,9 @@ impl Solution {
169163
```c
170164
int countPairs(int* nums, int numsSize, int k) {
171165
int ans = 0;
172-
for (int i = 0; i < numsSize - 1; i++) {
173-
for (int j = i + 1; j < numsSize; j++) {
174-
if (nums[i] == nums[j] && i * j % k == 0) {
175-
ans++;
176-
}
166+
for (int j = 1; j < numsSize; ++j) {
167+
for (int i = 0; i < j; ++i) {
168+
ans += (nums[i] == nums[j] && (i * j % k) == 0);
177169
}
178170
}
179171
return ans;
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
int countPairs(int* nums, int numsSize, int k) {
22
int ans = 0;
3-
for (int i = 0; i < numsSize - 1; i++) {
4-
for (int j = i + 1; j < numsSize; j++) {
5-
if (nums[i] == nums[j] && i * j % k == 0) {
6-
ans++;
7-
}
3+
for (int j = 1; j < numsSize; ++j) {
4+
for (int i = 0; i < j; ++i) {
5+
ans += (nums[i] == nums[j] && (i * j % k) == 0);
86
}
97
}
108
return ans;
11-
}
9+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
class Solution {
22
public:
33
int countPairs(vector<int>& nums, int k) {
4-
int n = nums.size();
54
int ans = 0;
6-
for (int i = 0; i < n; ++i) {
7-
for (int j = i + 1; j < n; ++j) {
8-
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
5+
for (int j = 1; j < nums.size(); ++j) {
6+
for (int i = 0; i < j; ++i) {
7+
ans += nums[i] == nums[j] && (i * j % k) == 0;
98
}
109
}
1110
return ans;
1211
}
13-
};
12+
};
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
func countPairs(nums []int, k int) int {
2-
n := len(nums)
3-
ans := 0
4-
for i, v := range nums {
5-
for j := i + 1; j < n; j++ {
6-
if v == nums[j] && (i*j)%k == 0 {
1+
func countPairs(nums []int, k int) (ans int) {
2+
for j, y := range nums {
3+
for i, x := range nums[:j] {
4+
if x == y && (i*j%k) == 0 {
75
ans++
86
}
97
}
108
}
11-
return ans
12-
}
9+
return
10+
}
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
class Solution {
22
public int countPairs(int[] nums, int k) {
3-
int n = nums.length;
43
int ans = 0;
5-
for (int i = 0; i < n; ++i) {
6-
for (int j = i + 1; j < n; ++j) {
7-
if (nums[i] == nums[j] && (i * j) % k == 0) {
8-
++ans;
9-
}
4+
for (int j = 1; j < nums.length; ++j) {
5+
for (int i = 0; i < j; ++i) {
6+
ans += nums[i] == nums[j] && (i * j % k) == 0 ? 1 : 0;
107
}
118
}
129
return ans;
1310
}
14-
}
11+
}
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
class Solution:
22
def countPairs(self, nums: List[int], k: int) -> int:
3-
n = len(nums)
4-
return sum(
5-
nums[i] == nums[j] and (i * j) % k == 0
6-
for i in range(n)
7-
for j in range(i + 1, n)
8-
)
3+
ans = 0
4+
for j in range(1, len(nums)):
5+
for i, x in enumerate(nums[:j]):
6+
ans += int(x == nums[j] and i * j % k == 0)
7+
return ans

solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
impl Solution {
22
pub fn count_pairs(nums: Vec<i32>, k: i32) -> i32 {
3-
let k = k as usize;
4-
let n = nums.len();
53
let mut ans = 0;
6-
for i in 0..n - 1 {
7-
for j in i + 1..n {
8-
if nums[i] == nums[j] && (i * j) % k == 0 {
4+
for j in 1..nums.len() {
5+
for (i, &x) in nums[..j].iter().enumerate() {
6+
if x == nums[j] && (i * j) as i32 % k == 0 {
97
ans += 1;
108
}
119
}

solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
function countPairs(nums: number[], k: number): number {
2-
const n = nums.length;
32
let ans = 0;
4-
for (let i = 0; i < n - 1; i++) {
5-
for (let j = i + 1; j < n; j++) {
3+
for (let j = 1; j < nums.length; ++j) {
4+
for (let i = 0; i < j; ++i) {
65
if (nums[i] === nums[j] && (i * j) % k === 0) {
7-
ans++;
6+
++ans;
87
}
98
}
109
}

solution/2100-2199/2177.Find Three Consecutive Integers That Sum to a Given Number/README_EN.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ tags:
5252

5353
<!-- solution:start -->
5454

55-
### Solution 1
55+
### Solution 1: Mathematics
56+
57+
Assume the three consecutive integers are $x-1$, $x$, and $x+1$. Their sum is $3x$, so $num$ must be a multiple of $3$. If $num$ is not a multiple of $3$, it cannot be expressed as the sum of three consecutive integers, and we return an empty array. Otherwise, let $x = \frac{num}{3}$, then $x-1$, $x$, and $x+1$ are the three consecutive integers whose sum is $num$.
58+
59+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
5660

5761
<!-- tabs:start -->
5862

0 commit comments

Comments
 (0)