40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
43
- ** 方法一:暴力枚举 **
43
+ ** 方法一:计数 **
44
44
45
- 枚举 ` arr ` 的每个元素 ` x ` ,判断 ` x+1 ` 是否在 ` arr ` 中,是则累加答案 。
45
+ 我们可以用一个哈希表或数组 $cnt$ 记录数组 $ arr$ 中的每个数出现的次数,然后遍历 $cnt$ 中的每个数 $x$,如果 $ x+1$ 也在 $cnt$ 中,那么就将 $cnt [ x ] $ 加到答案中 。
46
46
47
- 时间复杂度 $O(n^2)$,空间复杂度 $O(1)$。
48
-
49
- ** 方法二:哈希表**
50
-
51
- 将 ` arr ` 所有元素放入哈希表 ` s ` 中。然后遍历 ` arr ` 的每个元素 ` x ` ,判断 ` x+1 ` 是否在 ` s ` 中,是则累加答案。
52
-
53
- 时间复杂度 $O(n)$,空间复杂度 $O(n)$。
47
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $arr$ 的长度。
54
48
55
49
<!-- tabs:start -->
56
50
61
55
``` python
62
56
class Solution :
63
57
def countElements (self , arr : List[int ]) -> int :
64
- return sum (x + 1 in arr for x in arr)
65
- ```
66
-
67
- ``` python
68
- class Solution :
69
- def countElements (self , arr : List[int ]) -> int :
70
- s = set (arr)
71
- return sum (x + 1 in s for x in arr)
58
+ cnt = Counter(arr)
59
+ return sum (v for x, v in cnt.items() if cnt[x + 1 ])
72
60
```
73
61
74
62
### ** Java**
@@ -78,34 +66,17 @@ class Solution:
78
66
``` java
79
67
class Solution {
80
68
public int countElements (int [] arr ) {
81
- int ans = 0 ;
69
+ int [] cnt = new int [ 1001 ] ;
82
70
for (int x : arr) {
83
- for (int v : arr) {
84
- if (x + 1 == v) {
85
- ++ ans;
86
- break ;
87
- }
88
- }
71
+ ++ cnt[x];
89
72
}
90
- return ans;
91
- }
92
- }
93
- ```
94
-
95
- ``` java
96
- class Solution {
97
- public int countElements (int [] arr ) {
98
- Set<Integer > s = new HashSet<> ();
99
- for (int num : arr) {
100
- s. add(num);
101
- }
102
- int res = 0 ;
103
- for (int num : arr) {
104
- if (s. contains(num + 1 )) {
105
- ++ res;
73
+ int ans = 0 ;
74
+ for (int x = 0 ; x < 1000 ; ++ x) {
75
+ if (cnt[x + 1 ] > 0 ) {
76
+ ans += cnt[x];
106
77
}
107
78
}
108
- return res ;
79
+ return ans ;
109
80
}
110
81
}
111
82
```
@@ -116,28 +87,15 @@ class Solution {
116
87
class Solution {
117
88
public:
118
89
int countElements(vector<int >& arr) {
119
- int ans = 0 ;
90
+ int cnt [ 1001 ] {} ;
120
91
for (int x : arr) {
121
- for (int v : arr) {
122
- if (x + 1 == v) {
123
- ++ans;
124
- break;
125
- }
126
- }
92
+ ++cnt[ x] ;
127
93
}
128
- return ans;
129
- }
130
- };
131
- ```
132
-
133
- ```cpp
134
- class Solution {
135
- public:
136
- int countElements(vector<int>& arr) {
137
- unordered_set<int> s(arr.begin(), arr.end());
138
94
int ans = 0;
139
- for (int x : arr) {
140
- ans += s.count(x + 1);
95
+ for (int x = 0; x < 1000; ++x) {
96
+ if (cnt[ x + 1] ) {
97
+ ans += cnt[ x] ;
98
+ }
141
99
}
142
100
return ans;
143
101
}
@@ -147,72 +105,82 @@ public:
147
105
### **Go**
148
106
149
107
```go
150
- func countElements (arr []int ) int {
151
- ans := 0
152
- for _ , x := range arr {
153
- for _ , v := range arr {
154
- if x+1 == v {
155
- ans++
156
- break
157
- }
158
- }
159
- }
160
- return ans
161
- }
162
- ```
163
-
164
- ``` go
165
- func countElements (arr []int ) int {
166
- s := map [int ]bool {}
108
+ func countElements(arr []int) (ans int) {
109
+ mx := slices.Max(arr)
110
+ cnt := make([]int, mx+1)
167
111
for _, x := range arr {
168
- s [x] = true
112
+ cnt [x]++
169
113
}
170
- ans := 0
171
- for _ , x := range arr {
172
- if s[x+1 ] {
173
- ans++
114
+ for x := 0; x < mx; x++ {
115
+ if cnt[x+1] > 0 {
116
+ ans += cnt[x]
174
117
}
175
118
}
176
- return ans
119
+ return
177
120
}
178
121
```
179
122
180
- ### ** JavaScript **
123
+ ### ** TypeScript **
181
124
182
- ``` js
183
- /**
184
- * @param {number[]} arr
185
- * @return {number}
186
- */
187
- var countElements = function (arr ) {
188
- let ans = 0 ;
125
+ ``` ts
126
+ function countElements(arr : number []): number {
127
+ const mx = Math .max (... arr );
128
+ const cnt = Array (mx + 1 ).fill (0 );
189
129
for (const x of arr ) {
190
- ans += arr .includes (x + 1 );
130
+ ++ cnt [x ];
131
+ }
132
+ let ans = 0 ;
133
+ for (let i = 0 ; i < mx ; ++ i ) {
134
+ if (cnt [i + 1 ] > 0 ) {
135
+ ans += cnt [i ];
136
+ }
191
137
}
192
138
return ans ;
193
- };
139
+ }
194
140
```
195
141
142
+ ### ** JavaScript**
143
+
196
144
``` js
197
145
/**
198
146
* @param {number[]} arr
199
147
* @return {number}
200
148
*/
201
149
var countElements = function (arr ) {
202
- const s = new Set ();
150
+ const mx = Math .max (... arr);
151
+ const cnt = Array (mx + 1 ).fill (0 );
203
152
for (const x of arr) {
204
- s . add (x) ;
153
+ ++ cnt[x] ;
205
154
}
206
155
let ans = 0 ;
207
- for (const x of arr ) {
208
- if (s . has (x + 1 ) ) {
209
- ++ ans;
156
+ for (let i = 0 ; i < mx; ++ i ) {
157
+ if (cnt[i + 1 ] > 0 ) {
158
+ ans += cnt[i] ;
210
159
}
211
160
}
212
161
return ans;
213
162
};
214
163
```
215
164
165
+ ### ** Rust**
166
+
167
+ ``` rust
168
+ use std :: collections :: HashMap ;
169
+
170
+ impl Solution {
171
+ pub fn count_elements (arr : Vec <i32 >) -> i32 {
172
+ let mut cnt = HashMap :: new ();
173
+ for & num in & arr {
174
+ * cnt . entry (num ). or_insert (0 ) += 1 ;
175
+ }
176
+ cnt . iter ()
177
+ . filter (| (& x , _ )| cnt . contains_key (& (x + 1 )))
178
+ . map (| (_ , & v )| v )
179
+ . sum ()
180
+ }
181
+ }
182
+ ```
183
+
216
184
### ** PHP**
217
185
218
186
``` php
@@ -222,13 +190,14 @@ class Solution {
222
190
* @return Integer
223
191
*/
224
192
function countElements($arr) {
225
- $cnt = 0;
226
- for ($i = 0; $i < count($arr); $i++) {
227
- if (in_array($arr[$i] + 1, $arr)) {
228
- $cnt++;
193
+ $cnt = array_count_values($arr);
194
+ $ans = 0;
195
+ foreach ($cnt as $x => $v) {
196
+ if (isset($cnt[$x + 1])) {
197
+ $ans += $v;
229
198
}
230
199
}
231
- return $cnt++ ;
200
+ return $ans ;
232
201
}
233
202
}
234
203
```
0 commit comments