精華區beta Marginalman 關於我們 聯絡資訊
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
sustainer123: 大師 10/28 20:05