成都网站建设设计

将想法与焦点和您一起共享

go反转字符串(原地反转)

package main

import (
    "fmt"
)

func reverse(s string) string {
    s1 := []rune(s)
    for i := 0; i < len(s1)/2; i++ {
        tmp := s1[i]
        s1[i] = s1[len(s1)-1-i]
        s1[len(s1)-1-i] = tmp
    }
    return string(s1)
}

func main() {
    s := "123456"
    fmt.Println(reverse(s))
}

本文题目:go反转字符串(原地反转)
浏览地址:http://chengdu.cdxwcx.cn/article/pcdpge.html