Re: [閒聊] 每日leetcode

看板Marginalman作者 (早瀬ユウカの体操服 )時間9月前 (2025/03/09 15:43), 編輯推噓2(200)
留言2則, 2人參與, 9月前最新討論串1361/1552 (看更多)
https://leetcode.com/problems/alternating-groups-ii 3208. Alternating Groups II 給你一個包含0和1的陣列,0是紅色1是藍色,表示一個上色的圓環,如果連續k個顏色 兩兩都不同那他們就是一組Alternating Groups,求出共有幾組Alternating Groups。 思路: 1.這循環的索引判斷好麻煩ㄛ 所以我直接在原本的陣列後面append k-1個元素。 2.接下來就當普通的滑動窗口,如果當前加入的顏色沒有顏色不同就把窗口清空,最後 統計有幾個窗口就好。 Java Code: ----------------------------------------------------- class Solution { public int numberOfAlternatingGroups(int[] colors, int k) { int res = 0; int l = 0; int r = 0; int[] temp = new int[colors.length + (k - 1)]; for (int i = 0; i < colors.length; i++) { temp[i] = colors[i]; } for (int i = 0; i < (k - 1); i++) { temp[colors.length + i] = colors[i]; } while (r < temp.length) { r++; if (r - l >= 2 && temp[r - 1] == temp[r - 2]) { l = r - 1; } if (r - l > k) { l++; } if (r - l == k) { res++; } } return res; } } ----------------------------------------------------- -- https://i.imgur.com/9FXhO25.jpeg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 49.158.101.161 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1741506226.A.64E.html

03/09 15:53, 9月前 , 1F
大師
03/09 15:53, 1F

03/09 16:09, 9月前 , 2F
你很棒
03/09 16:09, 2F
文章代碼(AID): #1dpKQoPE (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dpKQoPE (Marginalman)