Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit efcefe3

Browse files
feat: add solutions to lc problem: No.1426 (#2059)
No.1426.Counting Elements
1 parent 25e1524 commit efcefe3

File tree

10 files changed

+228
-243
lines changed

10 files changed

+228
-243
lines changed

‎solution/1400-1499/1426.Counting Elements/README.md‎

Lines changed: 72 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,11 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43-
**方法一:暴力枚举**
43+
**方法一:计数**
4444

45-
枚举 `arr` 的每个元素 `x`,判断 `x+1` 是否在 `arr` 中,是则累加答案
45+
我们可以用一个哈希表或数组 $cnt$ 记录数组 $arr$ 中的每个数出现的次数,然后遍历 $cnt$ 中的每个数 $x,ドル如果 $x+1$ 也在 $cnt$ 中,那么就将 $cnt[x]$ 加到答案中
4646

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$ 的长度。
5448

5549
<!-- tabs:start -->
5650

@@ -61,14 +55,8 @@
6155
```python
6256
class Solution:
6357
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])
7260
```
7361

7462
### **Java**
@@ -78,34 +66,17 @@ class Solution:
7866
```java
7967
class Solution {
8068
public int countElements(int[] arr) {
81-
int ans = 0;
69+
int[] cnt = newint[1001];
8270
for (int x : arr) {
83-
for (int v : arr) {
84-
if (x + 1 == v) {
85-
++ans;
86-
break;
87-
}
88-
}
71+
++cnt[x];
8972
}
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];
10677
}
10778
}
108-
return res;
79+
return ans;
10980
}
11081
}
11182
```
@@ -116,28 +87,15 @@ class Solution {
11687
class Solution {
11788
public:
11889
int countElements(vector<int>& arr) {
119-
int ans = 0;
90+
int cnt[1001]{};
12091
for (int x : arr) {
121-
for (int v : arr) {
122-
if (x + 1 == v) {
123-
++ans;
124-
break;
125-
}
126-
}
92+
++cnt[x];
12793
}
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());
13894
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+
}
14199
}
142100
return ans;
143101
}
@@ -147,72 +105,82 @@ public:
147105
### **Go**
148106
149107
```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)
167111
for _, x := range arr {
168-
s[x] = true
112+
cnt[x]++
169113
}
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]
174117
}
175118
}
176-
return ans
119+
return
177120
}
178121
```
179122

180-
### **JavaScript**
123+
### **TypeScript**
181124

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);
189129
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+
}
191137
}
192138
return ans;
193-
};
139+
}
194140
```
195141

142+
### **JavaScript**
143+
196144
```js
197145
/**
198146
* @param {number[]} arr
199147
* @return {number}
200148
*/
201149
var countElements = function (arr) {
202-
const s = new Set();
150+
const mx = Math.max(...arr);
151+
const cnt = Array(mx + 1).fill(0);
203152
for (const x of arr) {
204-
s.add(x);
153+
++cnt[x];
205154
}
206155
let ans = 0;
207-
for (constxof 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];
210159
}
211160
}
212161
return ans;
213162
};
214163
```
215164

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+
216184
### **PHP**
217185

218186
```php
@@ -222,13 +190,14 @@ class Solution {
222190
* @return Integer
223191
*/
224192
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;
229198
}
230199
}
231-
return $cnt++;
200+
return $ans;
232201
}
233202
}
234203
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /