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