You are given a string s. Reorder the string using the following algorithm:
Pick the smallest character from s and append it to the result.
Pick the smallest character from s which is greater than the last appended character to the result and append it.
Repeat step 2 until you cannot pick more characters.
Pick the largest character from s and append it to the result.
Pick the largest character from s which is smaller than the last appended character to the result and append it.
Repeat step 5 until you cannot pick more characters.
Repeat the steps from 1 to 6 until you pick all characters from s.
In each step, If the smallest or the largest character appears more than once you can choose any occurrence and append it to the result.
Return the result string after sorting s with this algorithm.
Constraints:
1 <= s.length <= 500
s consists of only lowercase English letters.
給予一個字串
s
,使用下面的演算法重新排序:
- 從
s
拿一個最小的字元加到result
- 從
s
拿一個大於上一個加入的的字元的最小的字元加到result
- 重複步驟 2 直到不能再拿更多字元
- 從
s
拿一個最大的字元加到result
- 從
s
拿一個小於上一個加入的的字元的最大的字元加到result
- 重複步驟 5 直到不能再拿更多字元
- 重複 1 ~ 6 直到
s
的所有字元都被拿過回傳
result
,也就是經過演算法排序過的s
限制:
1 <= s.length <= 500
s
只會包含小寫英文字母
s
,降低演算法的複雜度LeetCode
C++