Skip to content

Latest commit

 

History

History
20 lines (19 loc) · 420 Bytes

剑指58II左旋转字符串.md

File metadata and controls

20 lines (19 loc) · 420 Bytes
func reverseLeftWords(s string, n int) string {
    b := []byte(s)
    // 1. 反转前n个字符
    reverse(&b, 0, n-1)
    // 2. 反转剩下的字符
    reverse(&b, n, len(b)-1)
    // 3. 反转s
    reverse(&b, 0, len(b)-1)
    return string(b)
}

func reverse(s *[]byte, start, end int) {
    for start < end {
        (*s)[start], (*s)[end] = (*s)[end], (*s)[start]
        start++
        end--
    }
}