Go語言程式:旋轉切片元素


切片與陣列類似,都是元素序列。但陣列是固定長度的元素序列,而切片是動態陣列,其大小可以改變。切片比陣列更高效、速度更快,並且它們是按引用傳遞而不是按值傳遞。這裡我們將學習使用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 - 使用Go語言中的`print`語句在控制檯上列印切片。

  • 步驟4 - 從`main`函式中呼叫`rotate`函式,傳入兩個引數:要旋轉的切片和旋轉的位數。

  • 步驟5 - 在`rotate`函式中,每次呼叫`reverse`函式時,一次向左,一次向右,最後是整個切片。

  • 步驟6 - 在`reverse`函式中,使用Go語言的多重迴圈特性反轉元素,切片中的值在特定位置反轉。

  • 步驟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`函式旋轉切片。在`rotate`函式中,將透過建立另一個臨時變數來複制值。讓我們看看演算法和程式碼是如何實現的。

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`函式和一個額外的臨時變數。因此,程式成功執行。

更新於:2023年2月13日

446 次瀏覽

開啟您的職業生涯

透過完成課程獲得認證

開始學習
廣告