反转字符串的切片

huangapple go评论122阅读模式
英文:

Reverse Slice of strings

问题

我不明白下面的实现有什么问题,我看了一下sort.StringSlice,它看起来是一样的。

  1. type RevStr []string
  2. func (s RevStr) Len() int { return len(s) }
  3. func (s RevStr) Less(i, j int) bool { return s[i] < s[j] }
  4. func (s RevStr) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  5. func Reverse(input string) string {
  6. rs := RevStr(strings.Split(input, " "))
  7. sort.Reverse(rs)
  8. return strings.Join(rs, " ")
  9. }
英文:

I don't understand what is wrong with the below implementation, I had a look at sort.StringSlice and it looks the same.

  1. type RevStr []string
  2. func(s RevStr) Len() int { return len(s) }
  3. func(s RevStr) Less(i, j int) bool { return s[i] &lt; s[j] }
  4. func(s RevStr) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
  5. func Reverse(input string) string {
  6. rs := RevStr(strings.Split(input, &quot; &quot;))
  7. sort.Reverse(rs)
  8. return strings.Join(rs, &quot; &quot;)
  9. }

答案1

得分: 16

sort.Reverse 不会对数据进行排序,而是返回一个新的 sort.Interface,该接口将以逆序对数据进行排序。所以你实际上不需要自己定义类型:

  1. func Reverse(input string) string {
  2. s := strings.Split(input, " ")
  3. sort.Sort(sort.Reverse(sort.StringSlice(s)))
  4. return strings.Join(s, " ")
  5. }

Playground: http://play.golang.org/p/w49FDCEHo3.

编辑: 如果你只需要对字符串切片进行反转,可以这样做:

  1. func reverse(ss []string) {
  2. last := len(ss) - 1
  3. for i := 0; i < len(ss)/2; i++ {
  4. ss[i], ss[last-i] = ss[last-i], ss[i]
  5. }
  6. }

Playground: http://play.golang.org/p/UptIRFV_SI

英文:

sort.Reverse doesn't sort the data, but rather returns a new sort.Interface that will sort the data in reverse order. So you don't really need your own type:

  1. func Reverse(input string) string {
  2. s := strings.Split(input, &quot; &quot;)
  3. sort.Sort(sort.Reverse(sort.StringSlice(s)))
  4. return strings.Join(s, &quot; &quot;)
  5. }

Playground: http://play.golang.org/p/w49FDCEHo3.

EDIT: If you just need to reverse a slice of strings, just do:

  1. func reverse(ss []string) {
  2. last := len(ss) - 1
  3. for i := 0; i &lt; len(ss)/2; i++ {
  4. ss[i], ss[last-i] = ss[last-i], ss[i]
  5. }
  6. }

Playground: http://play.golang.org/p/UptIRFV_SI

答案2

得分: 2

你的RevStr类型没有问题(尽管你可以直接使用sort.StringSlice)。你没有在反转的实现上调用sort.Sort

  1. package main
  2. import (
  3. "fmt"
  4. "sort"
  5. )
  6. func main() {
  7. s := []int{5, 2, 6, 3, 1, 4} // 未排序的切片
  8. sort.Sort(sort.Reverse(sort.IntSlice(s)))
  9. fmt.Println(s)
  10. }

你可以参考这个链接:https://golang.org/pkg/sort/#example_Reverse

英文:

Nothing is wrong with your RevStr type (though you could just use sort.StringSlice). You're not calling sort.Sort on the reversed implementation:

https://golang.org/pkg/sort/#example_Reverse

package main

  1. import (
  2. &quot;fmt&quot;
  3. &quot;sort&quot;
  4. )
  5. func main() {
  6. s := []int{5, 2, 6, 3, 1, 4} // unsorted
  7. sort.Sort(sort.Reverse(sort.IntSlice(s)))
  8. fmt.Println(s)
  9. }

答案3

得分: 2

一个一行代码的解决方案(使用lambda函数):

给定:

  1. myStrings := []string{"apple", "banana", "cherry"}

使用以下代码按照逆序排序:

  1. sort.Slice(myStrings, func(i, j int) bool { return myStrings[i] > myStrings[j]})

Playground示例:
https://play.golang.org/p/WZabAZTizHG

英文:

A one-liner solution (using a lambda):

Given:

  1. myStrings := []string{&quot;apple&quot;, &quot;banana&quot;, &quot;cherry&quot;}

Sort (in reverse order) with:

  1. sort.Slice(myStrings, func(i, j int) bool { return myStrings[i] &gt; myStrings[j]})

Playground Example:
https://play.golang.org/p/WZabAZTizHG

答案4

得分: 1

尽管@Ainar-G提供了一种反转字符串切片的方法,但我认为在for循环中使用两个变量进行反转更好。但这只是我的个人意见,是一种风格问题 反转字符串的切片

  1. func reverse(s []string) []string {
  2. for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
  3. s[i], s[j] = s[j], s[i]
  4. }
  5. return s
  6. }

使用示例的Playground链接:http://play.golang.org/p/v1Cy61NFv1

英文:

Although @Ainar-G has provided a way to reverse a slice of strings, I think it's nicer to use two variables in for loop to reverse. But it's only my personal opinion, a matter of style 反转字符串的切片

  1. func reverse(s []string) []string {
  2. for i, j := 0, len(s)-1; i &lt; j; i, j = i+1, j-1 {
  3. s[i], s[j] = s[j], s[i]
  4. }
  5. return s
  6. }

Playground link with example of usage: http://play.golang.org/p/v1Cy61NFv1

答案5

得分: 0

更简单的方法,不使用内置的排序功能:

  1. func reverse(s []string) []string {
  2. var result []string
  3. for i := len(s) - 1; i >= 0; i-- {
  4. result = append(result, s[i])
  5. }
  6. return result
  7. }

这段代码的作用是将字符串切片 s 中的元素反转,并返回反转后的结果。它通过遍历切片,从最后一个元素开始,逐个将元素添加到新的切片 result 中,最后返回 result

英文:

More simple way, without using built-in sorting feature :

  1. func reverse(s []string) []string {
  2. for i := len(s) - 1; i &gt;= 0; i-- {
  3. result = append(result, s[i])
  4. }
  5. return s
  6. }

答案6

得分: -1

func reverseStr(data []string) []string {
m := len(data) - 1
var out = []string{}
for i := m; i >= 0; i-- {
out = append(out, data[i])
}
return out
}

英文:
  1. func reverseStr(data []string) []string {
  2. m := len(data) - 1
  3. var out = []string{}
  4. for i := m; i &gt;= 0; i-- {
  5. out = append(out, data[i])
  6. }
  7. return out
  8. }

huangapple
  • 本文由 发表于 2016年1月16日 01:21:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/34816489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定