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 529d315

Browse files
authored
feat: add solutions to lc problem: No.2501 (doocs#3683)
1 parent b36a630 commit 529d315

File tree

4 files changed

+250
-0
lines changed

4 files changed

+250
-0
lines changed

‎solution/2500-2599/2501.Longest Square Streak in an Array/README.md‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,94 @@ func longestSquareStreak(nums []int) (ans int) {
291291

292292
<!-- solution:end -->
293293

294+
<!-- solution:start -->
295+
296+
### 方法三:计数
297+
298+
<!-- tabs:start -->
299+
300+
#### TypeScript
301+
302+
```ts
303+
function longestSquareStreak(nums: number[]): number {
304+
const cnt: Record<number, number> = {};
305+
const squares = new Set<number>();
306+
307+
for (const x of new Set(nums)) {
308+
cnt[x] = (cnt[x] ?? -1) + 1;
309+
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
310+
}
311+
312+
for (const key in cnt) {
313+
const x = +key;
314+
if (cnt[x] || cnt[x ** 2]) {
315+
squares.add(x);
316+
}
317+
}
318+
319+
if (squares.size <= 1) return -1;
320+
321+
const iterator = squares[Symbol.iterator]();
322+
let [max, c, x] = [0, 0, iterator.next().value];
323+
324+
while (x !== undefined) {
325+
if (squares.has(x)) {
326+
squares.delete(x);
327+
x **= 2;
328+
c++;
329+
} else {
330+
max = Math.max(max, c);
331+
x = iterator.next().value;
332+
c = 0;
333+
}
334+
}
335+
336+
return max;
337+
}
338+
```
339+
340+
#### JavaScript
341+
342+
```js
343+
function longestSquareStreak(nums) {
344+
const cnt = {};
345+
const squares = new Set();
346+
347+
for (const x of new Set(nums)) {
348+
cnt[x] = (cnt[x] ?? -1) + 1;
349+
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
350+
}
351+
352+
for (const key in cnt) {
353+
const x = +key;
354+
if (cnt[x] || cnt[x ** 2]) {
355+
squares.add(x);
356+
}
357+
}
358+
359+
if (squares.size <= 1) return -1;
360+
361+
const iterator = squares[Symbol.iterator]();
362+
let [max, c, x] = [0, 0, iterator.next().value];
363+
364+
while (x !== undefined) {
365+
if (squares.has(x)) {
366+
squares.delete(x);
367+
x **= 2;
368+
c++;
369+
} else {
370+
max = Math.max(max, c);
371+
x = iterator.next().value;
372+
c = 0;
373+
}
374+
}
375+
376+
return max;
377+
}
378+
```
379+
380+
<!-- tabs:end -->
381+
382+
<!-- solution:end -->
383+
294384
<!-- problem:end -->

‎solution/2500-2599/2501.Longest Square Streak in an Array/README_EN.md‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,4 +291,94 @@ func longestSquareStreak(nums []int) (ans int) {
291291

292292
<!-- solution:end -->
293293

294+
<!-- solution:start -->
295+
296+
### Solution 3: Counting
297+
298+
<!-- tabs:start -->
299+
300+
#### TypeScript
301+
302+
```ts
303+
function longestSquareStreak(nums: number[]): number {
304+
const cnt: Record<number, number> = {};
305+
const squares = new Set<number>();
306+
307+
for (const x of new Set(nums)) {
308+
cnt[x] = (cnt[x] ?? -1) + 1;
309+
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
310+
}
311+
312+
for (const key in cnt) {
313+
const x = +key;
314+
if (cnt[x] || cnt[x ** 2]) {
315+
squares.add(x);
316+
}
317+
}
318+
319+
if (squares.size <= 1) return -1;
320+
321+
const iterator = squares[Symbol.iterator]();
322+
let [max, c, x] = [0, 0, iterator.next().value];
323+
324+
while (x !== undefined) {
325+
if (squares.has(x)) {
326+
squares.delete(x);
327+
x **= 2;
328+
c++;
329+
} else {
330+
max = Math.max(max, c);
331+
x = iterator.next().value;
332+
c = 0;
333+
}
334+
}
335+
336+
return max;
337+
}
338+
```
339+
340+
#### JavaScript
341+
342+
```js
343+
function longestSquareStreak(nums) {
344+
const cnt = {};
345+
const squares = new Set();
346+
347+
for (const x of new Set(nums)) {
348+
cnt[x] = (cnt[x] ?? -1) + 1;
349+
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
350+
}
351+
352+
for (const key in cnt) {
353+
const x = +key;
354+
if (cnt[x] || cnt[x ** 2]) {
355+
squares.add(x);
356+
}
357+
}
358+
359+
if (squares.size <= 1) return -1;
360+
361+
const iterator = squares[Symbol.iterator]();
362+
let [max, c, x] = [0, 0, iterator.next().value];
363+
364+
while (x !== undefined) {
365+
if (squares.has(x)) {
366+
squares.delete(x);
367+
x **= 2;
368+
c++;
369+
} else {
370+
max = Math.max(max, c);
371+
x = iterator.next().value;
372+
c = 0;
373+
}
374+
}
375+
376+
return max;
377+
}
378+
```
379+
380+
<!-- tabs:end -->
381+
382+
<!-- solution:end -->
383+
294384
<!-- problem:end -->
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function longestSquareStreak(nums) {
2+
const cnt = {};
3+
const squares = new Set();
4+
5+
for (const x of new Set(nums)) {
6+
cnt[x] = (cnt[x] ?? -1) + 1;
7+
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
8+
}
9+
10+
for (const key in cnt) {
11+
const x = +key;
12+
if (cnt[x] || cnt[x ** 2]) {
13+
squares.add(x);
14+
}
15+
}
16+
17+
if (squares.size <= 1) return -1;
18+
19+
const iterator = squares[Symbol.iterator]();
20+
let [max, c, x] = [0, 0, iterator.next().value];
21+
22+
while (x !== undefined) {
23+
if (squares.has(x)) {
24+
squares.delete(x);
25+
x **= 2;
26+
c++;
27+
} else {
28+
max = Math.max(max, c);
29+
x = iterator.next().value;
30+
c = 0;
31+
}
32+
}
33+
34+
return max;
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function longestSquareStreak(nums: number[]): number {
2+
const cnt: Record<number, number> = {};
3+
const squares = new Set<number>();
4+
5+
for (const x of new Set(nums)) {
6+
cnt[x] = (cnt[x] ?? -1) + 1;
7+
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
8+
}
9+
10+
for (const key in cnt) {
11+
const x = +key;
12+
if (cnt[x] || cnt[x ** 2]) {
13+
squares.add(x);
14+
}
15+
}
16+
17+
if (squares.size <= 1) return -1;
18+
19+
const iterator = squares[Symbol.iterator]();
20+
let [max, c, x] = [0, 0, iterator.next().value];
21+
22+
while (x !== undefined) {
23+
if (squares.has(x)) {
24+
squares.delete(x);
25+
x **= 2;
26+
c++;
27+
} else {
28+
max = Math.max(max, c);
29+
x = iterator.next().value;
30+
c = 0;
31+
}
32+
}
33+
34+
return max;
35+
}

0 commit comments

Comments
(0)

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