Re: [闲聊] 每日leetcode

楼主: dont   2024-10-28 20:03:02
2501. Longest Square Streak in an Array
## 思路
转成set
检查2~sqrt(10**5)的int是否在set里面
再用while计算square个数
## Code
```python
class Solution:
def longestSquareStreak(self, nums: List[int]) -> int:
nums = set(nums)
res = 1
for i in range(2, 330):
if i not in nums:
continue
j, cnt = i, 1
while j * j in nums:
cnt += 1
j = j * j
res = max(res, cnt)
return -1 if res == 1 else res
```

Links booklink

Contact Us: admin [ a t ] ucptt.com