反转字符串的切片

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

Reverse Slice of strings

问题

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

type RevStr []string

func (s RevStr) Len() int { return len(s) }
func (s RevStr) Less(i, j int) bool { return s[i] < s[j] }
func (s RevStr) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func Reverse(input string) string {
  rs := RevStr(strings.Split(input, " "))
  sort.Reverse(rs)
  return strings.Join(rs, " ")
}
英文:

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

type RevStr []string

func(s RevStr) Len() int { return len(s) }
func(s RevStr) Less(i, j int) bool { return s[i] &lt; s[j] }
func(s RevStr) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

func Reverse(input string) string {
  rs := RevStr(strings.Split(input, &quot; &quot;))
  sort.Reverse(rs)
  return strings.Join(rs, &quot; &quot;)
}

答案1

得分: 16

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

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

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

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

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

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:

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

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

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

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

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

答案2

得分: 2

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

package main

import (
	"fmt"
	"sort"
)

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

你可以参考这个链接: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

import (
	&quot;fmt&quot;
	&quot;sort&quot;
)

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

答案3

得分: 2

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

给定:

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

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

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:

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

Sort (in reverse order) with:

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循环中使用两个变量进行反转更好。但这只是我的个人意见,是一种风格问题 反转字符串的切片

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

使用示例的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 反转字符串的切片

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

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

答案5

得分: 0

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

func reverse(s []string) []string {
    var result []string
    for i := len(s) - 1; i >= 0; i-- {
        result = append(result, s[i])
    }
    return result
}

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

英文:

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

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

答案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
}

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

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:

确定