Re: [閒聊] 每日leetcode

看板Marginalman作者 (單推凜寶)時間11月前 (2025/01/05 19:58), 編輯推噓0(001)
留言1則, 1人參與, 11月前最新討論串1245/1554 (看更多)
2381. Shifting Letters II 題目: 給定一個字串s 會有一個叫shifts的2d array 裡面每一項都是[start,end,direction] 意思就是從s的第start項到第end項每一項移動direction 而direction是0的時候要向左移(eg:a變成z) 相反direction是1的時候就向右移(eg:z變成a) 思路: 用一個矩陣delta來算s每一項要移動多少 在遍歷shifts的時候 如果右移就delta[start]+1 因為要加到end所以delta[end+1]-1 如果左移就相反 這樣對delta做完前綴和就是每一項移多少了 Code: char* shiftingLetters(char* s, int** shifts, int shiftsSize, int* shiftsColSize) { int delta[strlen(s) + 1]; memset(delta, 0, sizeof(delta)); for (int i = 0; i < shiftsSize; i++) { int start = shifts[i][0]; int end = shifts[i][1]; int direction = shifts[i][2]; delta[start] += (direction == 1) ? 1 : -1; if (end + 1 < strlen(s)) delta[end + 1] -= (direction == 1) ? 1 : -1; } int prefix = 0; for (int i = 0; i < strlen(s); i++) { prefix += delta[i]; s[i] = 'a' + (s[i] - 'a' + (prefix % 26 + 26) % 26) % 26; } return s; } 寫完發現沒用到shiftsColSize 不知道那個傳的是什麼捏 -- https://i.imgur.com/5XtXJd3.jpeg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.237.8.192 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1736078338.A.EEC.html

01/05 20:00, 11月前 , 1F
Oin會ㄇ
01/05 20:00, 1F
文章代碼(AID): #1dUdG2xi (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dUdG2xi (Marginalman)