Re: [閒聊] 每日leetcode
再來一題
這題超簡單
而且還是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
推
08/06 12:11,
1年前
, 1F
08/06 12:11, 1F
→
08/06 12:11,
1年前
, 2F
08/06 12:11, 2F
推
08/06 12:11,
1年前
, 3F
08/06 12:11, 3F
推
08/06 12:11,
1年前
, 4F
08/06 12:11, 4F
→
08/06 12:13,
1年前
, 5F
08/06 12:13, 5F
※ 編輯: oin1104 (61.230.129.159 臺灣), 08/06/2024 12:13:49
推
08/06 12:14,
1年前
, 6F
08/06 12:14, 6F
→
08/06 12:15,
1年前
, 7F
08/06 12:15, 7F
→
08/06 12:32,
1年前
, 8F
08/06 12:32, 8F
討論串 (同標題文章)
完整討論串 (本文為第 650 之 1548 篇):