asdasd
dsadsadasdunknown
golang
3 years ago
749 B
3
Indexable
Never
func swap(a, b *int) { tmp := *a + *b *b = tmp - *b *a = tmp - *b } func reverseNums(nums []int) { l, r := 0, len(nums)-1 for l <= r { swap(&nums[l], &nums[r]) l++ r-- } } func rotate(nums []int, k int) { if k == 0 { return } if k > len(nums) { if len(nums)%k == 0 { return } k -= len(nums) * (k / len(nums)) } h := int(len(nums) / 2) if k <= h { reverseNums(nums[0:k]) reverseNums(nums[k : len(nums)-k]) } else { a := len(nums) - k if a == 0 { return } module := k % a if module != 0 { reverseNums(nums[:module]) reverseNums(nums[module:a]) } else { reverseNums(nums[:a]) } } for i, j := 0, len(nums)-k; i < k; i, j = i+1, j+1 { swap(&nums[i], &nums[j]) } reverseNums(nums[k:]) }