Last active
January 22, 2019 02:24
-
-
Save schaitanya/7679828687ebd2de023398a07f66df62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rotate(nums []int, k int) { | |
k %=len(nums) | |
reverse(nums, 0, len(nums)-1) | |
reverse(nums, 0, k-1) | |
reverse(nums, k, len(nums)-1) | |
} | |
func reverse(nums []int, start, end int) { | |
for i, j := start, end; i < j; i, j = i+1, j-1 { | |
nums[i], nums[j] = nums[j], nums[i] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rotate(nums []int, k int) { | |
rand := make([]int, len(nums)) | |
copy(rand, nums) | |
for k > 0 { | |
rand = append(rand[len(rand)-1:], rand[:len(rand)-1]...) | |
k-- | |
} | |
for i, v := range rand { | |
nums[i] = v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment