精華區beta Marginalman 關於我們 聯絡資訊
再來一題 這題超簡單 而且還是hard 可以建立自信 趕緊的 @rainkaras https://i.imgur.com/KA4byZq.png 899. Orderly Queue You are given a string s and an integer k. You can choose one of the first k let ters of s and append it at the end of the string. Return the lexicographically smallest string you could have after applying the m entioned step any number of moves. 翻譯: 每次都可以把前k個字以內的字 丟到字串後面 問你最小的字串是什麼 最小的 = lexicographically smallest (把他當數字進位) 思路: 如果k==1的話 就只會有s.size()種字串 直接試試看就好 如果k>=2的話 每次都可以一直換換換 換到想要的位子 然後把那兩個字母前後順序交換 也就代表 只要次數多 沒有換不出來的字串 所以直接紀錄就好了 媽的 建立自信題 謝謝出題員 ```cpp class Solution { public: string orderlyQueue(string s, int k) { int n = s.size(); string res = s; if(k == 1) { string now = s; for(int i = 0 ; i < n ; i ++) { now.push_back(now[0]); now = now.substr(1); if(res > now) res = now; } return res; } int paper[26] = {}; for(char k : s) { paper[k-'a'] ++; } string res2; for(int i = 0 ; i < 26 ; i ++) { for(int j = 0 ; j < paper[i] ; j ++) { res2.push_back(i+'a'); } } return res2; } }; ``` -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.129.159 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1722917404.A.218.html
SydLrio: 你有什麼用08/06 12:11
oin1104: 你有什麼用08/06 12:11
amsmsk: 你有什麼用08/06 12:11
DJYOMIYAHINA: 我好崇拜你08/06 12:11
rrraaayyy: 卷哥08/06 12:13
※ 編輯: oin1104 (61.230.129.159 臺灣), 08/06/2024 12:13:49
steven183: 別卷了 08/06 12:14
a9486l: 大師 08/06 12:15
dont: 大師 08/06 12:32