Re: [閒聊] 每日leetcode
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
```
--
https://i.imgur.com/kyBhy6o.jpeg

--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.173 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1730116986.A.A01.html
推
10/28 20:05,
1年前
, 1F
10/28 20:05, 1F
討論串 (同標題文章)
完整討論串 (本文為第 1056 之 1554 篇):