标签:翻转单词顺序 ice var slice for sort 单词 spl 翻转单词
package main import ( "fmt" "strings" ) func main() { /* 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" */ var s = "the sky is blue" fmt.Println(reverseWords(s)) } func reverseWords(s string) string { str := strings.Split(s, " ") // sort.Sort(sort.Reverse(sort.StringSlice(str))) // 为毛不行 var ss []string for i := len(str) - 1; i >= 0; i-- { fmt.Println(str[i]) ss = append(ss, (str[i])) } var newStr string for i := 0; i < len(ss); i++ { newStr += ss[i] + " " } return newStr }
标签:翻转单词顺序 ice var slice for sort 单词 spl 翻转单词
原文地址:https://www.cnblogs.com/yzg-14/p/13358274.html