File tree Expand file tree Collapse file tree 18 files changed +435
-6
lines changed
2176.Count Equal and Divisible Pairs in an Array
2177.Find Three Consecutive Integers That Sum to a Given Number
2178.Maximum Split of Positive Even Integers Expand file tree Collapse file tree 18 files changed +435
-6
lines changed Original file line number Diff line number Diff line change 42
42
43
43
<!-- 这里可写通用的实现逻辑 -->
44
44
45
+ ** 方法一:暴力枚举**
46
+
45
47
<!-- tabs:start -->
46
48
47
49
### ** Python3**
48
50
49
51
<!-- 这里可写当前语言的特殊实现逻辑 -->
50
52
51
53
``` 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))
53
58
```
54
59
55
60
### ** Java**
56
61
57
62
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
63
59
64
``` 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
+ ```
60
100
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
+ }
61
116
```
62
117
63
118
### ** TypeScript**
Original file line number Diff line number Diff line change @@ -43,13 +43,66 @@ There are 4 pairs that meet all the requirements:
43
43
### ** Python3**
44
44
45
45
``` 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))
47
50
```
48
51
49
52
### ** Java**
50
53
51
54
``` 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
+ ```
52
90
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
+ }
53
106
```
54
107
55
108
### ** TypeScript**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 ))
Original file line number Diff line number Diff line change 37
37
38
38
<!-- 这里可写通用的实现逻辑 -->
39
39
40
+ ** 方法一:数学**
41
+
40
42
<!-- tabs:start -->
41
43
42
44
### ** Python3**
43
45
44
46
<!-- 这里可写当前语言的特殊实现逻辑 -->
45
47
46
48
``` 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 ]
48
53
```
49
54
50
55
### ** Java**
51
56
52
57
<!-- 这里可写当前语言的特殊实现逻辑 -->
53
58
54
59
``` 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**
55
85
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
+ }
56
94
```
57
95
58
96
### ** TypeScript**
Original file line number Diff line number Diff line change 38
38
### ** Python3**
39
39
40
40
``` 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 ]
42
45
```
43
46
44
47
### ** Java**
45
48
46
49
``` 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**
47
75
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
+ }
48
84
```
49
85
50
86
### ** TypeScript**
Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments