Skip to content

Commit 18e1b98

Browse files
committed
feat: add solutions to lc problems
* No.2176.Count Equal and Divisible Pairs in an Array * No.2177.Find Three Consecutive Integers That Sum to a Given Number * No.2178.Maximum Split of Positive Even Integers
1 parent 4cf9c86 commit 18e1b98

File tree

18 files changed

+435
-6
lines changed

18 files changed

+435
-6
lines changed

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

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,77 @@
4242

4343
<!-- 这里可写通用的实现逻辑 -->
4444

45+
**方法一:暴力枚举**
46+
4547
<!-- tabs:start -->
4648

4749
### **Python3**
4850

4951
<!-- 这里可写当前语言的特殊实现逻辑 -->
5052

5153
```python
52-
54+
class Solution:
55+
def countPairs(self, nums: List[int], k: int) -> int:
56+
n = len(nums)
57+
return sum(nums[i] == nums[j] and (i * j) % k == 0 for i in range(n) for j in range(i + 1, n))
5358
```
5459

5560
### **Java**
5661

5762
<!-- 这里可写当前语言的特殊实现逻辑 -->
5863

5964
```java
65+
class Solution {
66+
public int countPairs(int[] nums, int k) {
67+
int n = nums.length;
68+
int ans = 0;
69+
for (int i = 0; i < n; ++i) {
70+
for (int j = i + 1; j < n; ++j) {
71+
if (nums[i] == nums[j] && (i * j) % k == 0) {
72+
++ans;
73+
}
74+
}
75+
}
76+
return ans;
77+
}
78+
}
79+
```
80+
81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int countPairs(vector<int>& nums, int k) {
87+
int n = nums.size();
88+
int ans = 0;
89+
for (int i = 0; i < n; ++i)
90+
{
91+
for (int j = i + 1; j < n; ++j)
92+
{
93+
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
94+
}
95+
}
96+
return ans;
97+
}
98+
};
99+
```
60100
101+
### **Go**
102+
103+
```go
104+
func countPairs(nums []int, k int) int {
105+
n := len(nums)
106+
ans := 0
107+
for i, v := range nums {
108+
for j := i + 1; j < n; j++ {
109+
if v == nums[j] && (i*j)%k == 0 {
110+
ans++
111+
}
112+
}
113+
}
114+
return ans
115+
}
61116
```
62117

63118
### **TypeScript**

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,66 @@ There are 4 pairs that meet all the requirements:
4343
### **Python3**
4444

4545
```python
46-
46+
class Solution:
47+
def countPairs(self, nums: List[int], k: int) -> int:
48+
n = len(nums)
49+
return sum(nums[i] == nums[j] and (i * j) % k == 0 for i in range(n) for j in range(i + 1, n))
4750
```
4851

4952
### **Java**
5053

5154
```java
55+
class Solution {
56+
public int countPairs(int[] nums, int k) {
57+
int n = nums.length;
58+
int ans = 0;
59+
for (int i = 0; i < n; ++i) {
60+
for (int j = i + 1; j < n; ++j) {
61+
if (nums[i] == nums[j] && (i * j) % k == 0) {
62+
++ans;
63+
}
64+
}
65+
}
66+
return ans;
67+
}
68+
}
69+
```
70+
71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
int countPairs(vector<int>& nums, int k) {
77+
int n = nums.size();
78+
int ans = 0;
79+
for (int i = 0; i < n; ++i)
80+
{
81+
for (int j = i + 1; j < n; ++j)
82+
{
83+
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
84+
}
85+
}
86+
return ans;
87+
}
88+
};
89+
```
5290
91+
### **Go**
92+
93+
```go
94+
func countPairs(nums []int, k int) int {
95+
n := len(nums)
96+
ans := 0
97+
for i, v := range nums {
98+
for j := i + 1; j < n; j++ {
99+
if v == nums[j] && (i*j)%k == 0 {
100+
ans++
101+
}
102+
}
103+
}
104+
return ans
105+
}
53106
```
54107

55108
### **TypeScript**
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int countPairs(vector<int>& nums, int k) {
4+
int n = nums.size();
5+
int ans = 0;
6+
for (int i = 0; i < n; ++i)
7+
{
8+
for (int j = i + 1; j < n; ++j)
9+
{
10+
if (nums[i] == nums[j] && (i * j) % k == 0) ++ans;
11+
}
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 {
7+
ans++
8+
}
9+
}
10+
}
11+
return ans
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int countPairs(int[] nums, int k) {
3+
int n = nums.length;
4+
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+
}
10+
}
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def countPairs(self, nums: List[int], k: int) -> int:
3+
n = len(nums)
4+
return sum(nums[i] == nums[j] and (i * j) % k == 0 for i in range(n) for j in range(i + 1, n))

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,60 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
**方法一:数学**
41+
4042
<!-- tabs:start -->
4143

4244
### **Python3**
4345

4446
<!-- 这里可写当前语言的特殊实现逻辑 -->
4547

4648
```python
47-
49+
class Solution:
50+
def sumOfThree(self, num: int) -> List[int]:
51+
a, b = divmod(num, 3)
52+
return [] if b else [a - 1, a, a + 1]
4853
```
4954

5055
### **Java**
5156

5257
<!-- 这里可写当前语言的特殊实现逻辑 -->
5358

5459
```java
60+
class Solution {
61+
public long[] sumOfThree(long num) {
62+
if (num % 3 != 0) {
63+
return new long[]{};
64+
}
65+
long x = num / 3;
66+
return new long[]{x - 1, x, x + 1};
67+
}
68+
}
69+
```
70+
71+
### **C++**
72+
73+
```cpp
74+
class Solution {
75+
public:
76+
vector<long long> sumOfThree(long long num) {
77+
if (num % 3) return {};
78+
long long x = num / 3;
79+
return {x - 1, x, x + 1};
80+
}
81+
};
82+
```
83+
84+
### **Go**
5585
86+
```go
87+
func sumOfThree(num int64) []int64 {
88+
if num%3 != 0 {
89+
return []int64{}
90+
}
91+
x := num / 3
92+
return []int64{x - 1, x, x + 1}
93+
}
5694
```
5795

5896
### **TypeScript**

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,49 @@
3838
### **Python3**
3939

4040
```python
41-
41+
class Solution:
42+
def sumOfThree(self, num: int) -> List[int]:
43+
a, b = divmod(num, 3)
44+
return [] if b else [a - 1, a, a + 1]
4245
```
4346

4447
### **Java**
4548

4649
```java
50+
class Solution {
51+
public long[] sumOfThree(long num) {
52+
if (num % 3 != 0) {
53+
return new long[]{};
54+
}
55+
long x = num / 3;
56+
return new long[]{x - 1, x, x + 1};
57+
}
58+
}
59+
```
60+
61+
### **C++**
62+
63+
```cpp
64+
class Solution {
65+
public:
66+
vector<long long> sumOfThree(long long num) {
67+
if (num % 3) return {};
68+
long long x = num / 3;
69+
return {x - 1, x, x + 1};
70+
}
71+
};
72+
```
73+
74+
### **Go**
4775
76+
```go
77+
func sumOfThree(num int64) []int64 {
78+
if num%3 != 0 {
79+
return []int64{}
80+
}
81+
x := num / 3
82+
return []int64{x - 1, x, x + 1}
83+
}
4884
```
4985

5086
### **TypeScript**
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public:
3+
vector<long long> sumOfThree(long long num) {
4+
if (num % 3) return {};
5+
long long x = num / 3;
6+
return {x - 1, x, x + 1};
7+
}
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func sumOfThree(num int64) []int64 {
2+
if num%3 != 0 {
3+
return []int64{}
4+
}
5+
x := num / 3
6+
return []int64{x - 1, x, x + 1}
7+
}

0 commit comments

Comments
 (0)