Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間1年前 (2024/10/28 20:03), 編輯推噓1(100)
留言1則, 1人參與, 1年前最新討論串1056/1554 (看更多)
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
文章代碼(AID): #1d7trwe1 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1d7trwe1 (Marginalman)