Skip to content

Latest commit

 

History

History
32 lines (26 loc) · 867 Bytes

899. 有序队列.md

File metadata and controls

32 lines (26 loc) · 867 Bytes

899. 有序队列

题目传送门

点击这里

解题思路

这道题其实算是脑筋急转弯的题,这里的k要分两种情况,一种是k=1,这样子的话,其实就是个循环的字符串,每次都可以将头节点放到最后,所以我们就可以遍历所有情况找到最小的返回,而k>1的情况,必能得到一个字符升序排列,即最小的字典序字符串。

代码

func orderlyQueue(s string, k int) string {
    var ans string
    if k == 1 {
        ans = s
        for i := 1;i < len(s);i ++ {
            s = s[1:] + s[:1]
            if s < ans {
                ans = s
            }
        }
        return ans
    }
    b := []byte(s)
    sort.Slice(b, func(i, j int) bool {
        return b[i] < b[j]
    })
    return string(b)
}