Go中的旋转阵列 [英] Rotate Array in Go
问题描述
这是LeetCode问题: 189.旋转阵列:
给出一个数组,将数组向右旋转k步,其中k为非负的.
示例1:
输入:[1,2,3,4,5,6,7]且k = 3
输出:[5,6,7,1,2,3,4]
这是我的解决方案:
func rotation(nums [] int,k int){k = k%len(数字)nums = append(nums [k:],nums [0:k] ...)fmt.Println(数字)}
这是一种简单明了的算法,但是不起作用.
我是Go的新手.我想 nums
是按值传递的,对 nums
的更改不会影响真正的 nums
.
我该如何正确处理?
在Go中,所有参数均按值传递.
Go切片在运行时由切片描述符表示:
type slice struct {数组不安全len intcap int}
如果您更改了函数中的任何切片描述符值,则通常通过返回更改后的切片描述符来传达更改.
您的 rotate
函数更改了指向基础数组的切片 num
指针的值和切片的容量,因此返回 num
.>
例如,我修复了 rotate
算法中的错误后,
程序包主要导入"fmt"func rotation(nums [] int,k int)[] int {如果k <0 ||len(nums)== 0 {返回数字}fmt.Printf(数字%p数组%p len%d上限%d切片%v \ n",& nums,& nums [0],len(nums),cap(nums),nums)r:= len(数字)-k%len(数字)nums = append(nums [r:],nums [:r] ...)fmt.Printf(数字%p数组%p len%d上限%d切片%v \ n",& nums,& nums [0],len(nums),cap(nums),nums)返回数字}func main(){nums:= [] int {1、2、3、4、5、6、7}fmt.Printf(数字%p数组%p len%d上限%d切片%v \ n",& nums,& nums [0],len(nums),cap(nums),nums)nums =旋转(nums,3)fmt.Printf(数字%p数组%p len%d上限%d切片%v \ n",& nums,& nums [0],len(nums),cap(nums),nums)}
输出:
数字0xc00000a080数组0xc00001a1c0 len 7 cap 7 slice [1 2 3 4 5 6 7]nums 0xc00000a0c0数组0xc00001a1c0 len 7 cap 7 slice [1 2 3 4 5 6 7]nums 0xc00000a0c0数组0xc00001a240 len 7 cap 8 slice [5 6 7 1 2 3 4]nums 0xc00000a080数组0xc00001a240 len 7 cap 8 slice [5 6 7 1 2 3 4]
参考: The Go博客:Go Slices:用法和内部原理
This is a LeetCode problem: 189. Rotate Array:
Given an array, rotate the array to the right by k steps, where k is non-negative.
Example 1:
Input: [1,2,3,4,5,6,7] and k = 3
Output: [5,6,7,1,2,3,4]
And here is my solution:
func rotate(nums []int, k int) {
k = k % len(nums)
nums = append(nums[k:],nums[0:k]...)
fmt.Println(nums)
}
It is a straight forward algorithm but it does not work.
I am new to Go. I suppose nums
is passed by value and changes to nums
won't affect the real nums
.
How can I get this right?
In Go, all arguments are passed by value.
A Go slice is represented at runtime by a slice descriptor:
type slice struct {
array unsafe.Pointer
len int
cap int
}
If you change any of the slice descriptor values in a function then communicate the change, typically by returning the changed slice descriptor.
Your rotate
function changes the values of the slice num
pointer to the underlying array and the slice capacity, so return num
.
For example, after I fixed the bugs in your rotate
algorithm,
package main
import "fmt"
func rotate(nums []int, k int) []int {
if k < 0 || len(nums) == 0 {
return nums
}
fmt.Printf("nums %p array %p len %d cap %d slice %v\n", &nums, &nums[0], len(nums), cap(nums), nums)
r := len(nums) - k%len(nums)
nums = append(nums[r:], nums[:r]...)
fmt.Printf("nums %p array %p len %d cap %d slice %v\n", &nums, &nums[0], len(nums), cap(nums), nums)
return nums
}
func main() {
nums := []int{1, 2, 3, 4, 5, 6, 7}
fmt.Printf("nums %p array %p len %d cap %d slice %v\n", &nums, &nums[0], len(nums), cap(nums), nums)
nums = rotate(nums, 3)
fmt.Printf("nums %p array %p len %d cap %d slice %v\n", &nums, &nums[0], len(nums), cap(nums), nums)
}
Output:
nums 0xc00000a080 array 0xc00001a1c0 len 7 cap 7 slice [1 2 3 4 5 6 7]
nums 0xc00000a0c0 array 0xc00001a1c0 len 7 cap 7 slice [1 2 3 4 5 6 7]
nums 0xc00000a0c0 array 0xc00001a240 len 7 cap 8 slice [5 6 7 1 2 3 4]
nums 0xc00000a080 array 0xc00001a240 len 7 cap 8 slice [5 6 7 1 2 3 4]
Reference: The Go Blog: Go Slices: usage and internals
这篇关于Go中的旋转阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!