循环移动数组
循环移动数组
给定一个数组和正整数n(n 小于数组长度), 请求此数组循环左移n个位置, 要求空间复杂度O(1)
例子:
输入: [1, 2, 3, 4, 5] 2
输出: [3, 4, 5, 1, 2]
func moveArrary(arr []int, n int) []int {
for i := 0; i < n/2; i++ {
arr[i], arr[n-i-1] = arr[n-i-1], arr[i]
}
fmt.Println(arr)
length := len(arr)
for i := n; i < (n+(length-n)/2); i++ {
fmt.Println(i, length-1-i+n)
arr[i], arr[length-1-i+n] = arr[length-1-i+n], arr[i]
}
fmt.Println(arr)
for i := 0; i <= length/2; i++ {
arr[i], arr[length-1-i] = arr[length-1-i], arr[i]
}
return arr
}