Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,94 @@ func longestSquareStreak(nums []int) (ans int) {

<!-- solution:end -->

<!-- solution:start -->

### 方法三:计数

<!-- tabs:start -->

#### TypeScript

```ts
function longestSquareStreak(nums: number[]): number {
const cnt: Record<number, number> = {};
const squares = new Set<number>();

for (const x of new Set(nums)) {
cnt[x] = (cnt[x] ?? -1) + 1;
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
}

for (const key in cnt) {
const x = +key;
if (cnt[x] || cnt[x ** 2]) {
squares.add(x);
}
}

if (squares.size <= 1) return -1;

const iterator = squares[Symbol.iterator]();
let [max, c, x] = [0, 0, iterator.next().value];

while (x !== undefined) {
if (squares.has(x)) {
squares.delete(x);
x **= 2;
c++;
} else {
max = Math.max(max, c);
x = iterator.next().value;
c = 0;
}
}

return max;
}
```

#### JavaScript

```js
function longestSquareStreak(nums) {
const cnt = {};
const squares = new Set();

for (const x of new Set(nums)) {
cnt[x] = (cnt[x] ?? -1) + 1;
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
}

for (const key in cnt) {
const x = +key;
if (cnt[x] || cnt[x ** 2]) {
squares.add(x);
}
}

if (squares.size <= 1) return -1;

const iterator = squares[Symbol.iterator]();
let [max, c, x] = [0, 0, iterator.next().value];

while (x !== undefined) {
if (squares.has(x)) {
squares.delete(x);
x **= 2;
c++;
} else {
max = Math.max(max, c);
x = iterator.next().value;
c = 0;
}
}

return max;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -291,4 +291,94 @@ func longestSquareStreak(nums []int) (ans int) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 3: Counting

<!-- tabs:start -->

#### TypeScript

```ts
function longestSquareStreak(nums: number[]): number {
const cnt: Record<number, number> = {};
const squares = new Set<number>();

for (const x of new Set(nums)) {
cnt[x] = (cnt[x] ?? -1) + 1;
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
}

for (const key in cnt) {
const x = +key;
if (cnt[x] || cnt[x ** 2]) {
squares.add(x);
}
}

if (squares.size <= 1) return -1;

const iterator = squares[Symbol.iterator]();
let [max, c, x] = [0, 0, iterator.next().value];

while (x !== undefined) {
if (squares.has(x)) {
squares.delete(x);
x **= 2;
c++;
} else {
max = Math.max(max, c);
x = iterator.next().value;
c = 0;
}
}

return max;
}
```

#### JavaScript

```js
function longestSquareStreak(nums) {
const cnt = {};
const squares = new Set();

for (const x of new Set(nums)) {
cnt[x] = (cnt[x] ?? -1) + 1;
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
}

for (const key in cnt) {
const x = +key;
if (cnt[x] || cnt[x ** 2]) {
squares.add(x);
}
}

if (squares.size <= 1) return -1;

const iterator = squares[Symbol.iterator]();
let [max, c, x] = [0, 0, iterator.next().value];

while (x !== undefined) {
if (squares.has(x)) {
squares.delete(x);
x **= 2;
c++;
} else {
max = Math.max(max, c);
x = iterator.next().value;
c = 0;
}
}

return max;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function longestSquareStreak(nums) {
const cnt = {};
const squares = new Set();

for (const x of new Set(nums)) {
cnt[x] = (cnt[x] ?? -1) + 1;
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
}

for (const key in cnt) {
const x = +key;
if (cnt[x] || cnt[x ** 2]) {
squares.add(x);
}
}

if (squares.size <= 1) return -1;

const iterator = squares[Symbol.iterator]();
let [max, c, x] = [0, 0, iterator.next().value];

while (x !== undefined) {
if (squares.has(x)) {
squares.delete(x);
x **= 2;
c++;
} else {
max = Math.max(max, c);
x = iterator.next().value;
c = 0;
}
}

return max;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function longestSquareStreak(nums: number[]): number {
const cnt: Record<number, number> = {};
const squares = new Set<number>();

for (const x of new Set(nums)) {
cnt[x] = (cnt[x] ?? -1) + 1;
cnt[x ** 2] = (cnt[x ** 2] ?? -1) + 1;
}

for (const key in cnt) {
const x = +key;
if (cnt[x] || cnt[x ** 2]) {
squares.add(x);
}
}

if (squares.size <= 1) return -1;

const iterator = squares[Symbol.iterator]();
let [max, c, x] = [0, 0, iterator.next().value];

while (x !== undefined) {
if (squares.has(x)) {
squares.delete(x);
x **= 2;
c++;
} else {
max = Math.max(max, c);
x = iterator.next().value;
c = 0;
}
}

return max;
}
Loading