Re: [閒聊] 每日leetcode

看板Marginalman作者 (dont)時間11月前 (2025/01/05 11:29), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1244/1554 (看更多)
2381. Shifting Letters II ## 思路 shift: [start, end] 範圍的字元 +/- 1 先掃shifts 記錄 start/end+1 的變化值 再掃string index時, 更新目前要轉換的總變化值 跟字元 ## Code ```cpp class Solution { public: string shiftingLetters(string s, vector<vector<int>>& shifts) { int n = s.size(); vector<int> change(n+1, 0); for (vector<int>& shift: shifts) { int delta = shift[2] ? 1 : -1; change[shift[0]] += delta; change[shift[1]+1] -= delta; } int curr = 0; for (int i=0; i<n; ++i) { curr = (curr + change[i]) % 26; s[i] = 'a' + (s[i] - 'a' + curr + 26) % 26; } return s; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 86.48.13.245 (日本) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1736047762.A.A49.html
文章代碼(AID): #1dUVoIf9 (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1dUVoIf9 (Marginalman)