Golang 切片旋转元素

Golang 切片旋转元素

切片是一系列元素,就像数组一样。数组是一个固定顺序的元素序列,而切片是一个动态数组,这意味着它的值是可变的并且可以更改。切片比数组更高效和更快,此外,它们是按引用而不是按值传递的。在这里,我们将学习使用Go编程语言来旋转切片的各种技术。

语法

func append(slice, element_1, element_2…, element_N) []T

append函数用于将值添加到数组切片中。它接收多个参数。第一个参数是我们要添加值的数组,然后是要添加的值。函数然后返回包含所有值的最终数组切片。

func append(slice, element_1, element_2…, element_N) []T

append函数用于向数组切片添加值。它接受多个参数。第一个参数是我们希望添加值的数组,接着是要添加的值。然后,函数返回包含所有值的最终数组切片。

func copy(dst, str[] type) int

go语言中的copy函数用于将一个源数组的值复制到目标数组,并返回复制的元素数量作为结果。它以两个数组作为参数。

func make ([] type, size, capacity)

在Go语言中,make函数用于创建数组/映射,它接受要创建的变量类型、大小和容量作为参数。

步骤

  • 步骤1 - 创建一个main包,并在程序中声明fmt(格式化包)包,其中main产生可执行代码,fmt用于格式化输入和输出。

  • 步骤2 - 创建一个main函数,在该函数中创建一个切片,并使用append函数向其添加元素。

  • 步骤3 - 使用Golang的print语句在控制台上打印切片。

  • 步骤4 - 在main函数中调用rotate函数,并传入两个参数:一个是要旋转的切片,另一个是切片旋转的位置数。

  • 步骤5 - 在rotate函数中,使用不同的参数(分别是左旋一次、右旋一次和整个切片)调用reverse函数。

  • 步骤6 - 在reverse函数中,使用Golang的多重循环特性反转元素和切片中特定位置的值。

  • 步骤7 - 切片旋转后,使用fmt.Println()函数在控制台上打印切片,其中ln表示换行。

示例1

在此示例中,我们将学习如何通过在不同位置反转切片,然后反转整个切片来旋转切片。

package main
import (
   "fmt"
)
func rotate(slice []int, positions int) {
   positions = positions % len(slice) //find the position
   reverse(slice[:positions]) //reverse the beginning elements
   reverse(slice[positions:]) //reverse the end elements
   reverse(slice) //reverse the entire slice
}
func reverse(slice []int) {
   for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
      slice[i], slice[j] = slice[j], slice[i]
   }
}
func main() {
   var slice []int // initialize slice
   slice = append(slice, 10) //fill the slice using append function
   slice = append(slice, 20)
   slice = append(slice, 30)
   slice = append(slice, 40)
   slice = append(slice, 50)
   fmt.Println("The original slice created here is: ", slice)
   rotate(slice, 2) //call the rotate function
   fmt.Println("The rotated slice is: ", slice)
}

输出

The original slice created here is:  [10 20 30 40 50]
The rotated slice is:  [30 40 50 10 20]

示例2

在这个示例中,我们将学习如何使用内置的copy函数旋转一个切片。在旋转函数中,值将通过创建另一个临时变量来进行复制。让我们看一下算法和代码,看看如何完成。

package main
import (
   "fmt"
)
func rotate(slice []int, position int) {
   position = position % len(slice) //find position
   temp := make([]int, position) //create a temp slice to store values
   copy(temp, slice[:position])
   copy(slice, slice[position:])
   copy(slice[len(slice)-position:], temp) //copy elements from temp to end of slice
}
func main() {
   var slice []int // initialize slice
   slice = append(slice, 10) //fill the slice using append function
   slice = append(slice, 20)
   slice = append(slice, 30)
   slice = append(slice, 40)
   slice = append(slice, 50)
   fmt.Println("The original slice created here is: ", slice)
   rotate(slice, 2)
   fmt.Println("The rotated slice is: ", slice) //print rotated slice
}

输出

The original slice created here is:  [10 20 30 40 50]
The rotated slice is:  [30 40 50 10 20]

结论

我们使用两个示例执行了切片旋转的程序。在第一个示例中,我们使用了reverse函数来旋转切片,在第二个示例中,我们使用了copy函数和一个额外的临时变量。因此,程序成功执行。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程