Re: [閒聊] 每日leetcode

看板Marginalman作者 (神楽めあ的錢包)時間9月前 (2025/03/09 16:13), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1362/1553 (看更多)
好久沒發每日文了 最近的每日都偏簡單 3208. Alternating Groups II 這題就對阿 直接把前k-1個元素街道colors後面 接著就把colors[i] xor colors[i-1] 如果結果是1,就把cnt++ 當cnt == k,答案就+1 如果結果是0,cnt重置為1 這樣就可以得到答案了 golang code func numberOfAlternatingGroups(colors []int, k int) int { colors = append(colors, colors[:k-1]...) cnt, ans := 1, 0 for i := 1; i < len(colors); i++ { if colors[i]^colors[i-1] == 1 { cnt++ if cnt == k { ans++ cnt-- } } else { cnt = 1 } } return ans } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.82.143.172 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1741507980.A.D49.html
文章代碼(AID): #1dpKsCr9 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dpKsCr9 (Marginalman)