精華區beta Marginalman 關於我們 聯絡資訊
好久沒發每日文了 最近的每日都偏簡單 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