You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The exercise requested to write a version of rotate that operates in a single pass. The answer provided rotates a slice by one position to the left, so while your code runs correctly it's not the right answer to the exercise.
Generic solution that performs in place rotation
// Rotates in place the slice s to the left by n stepsfuncrotateLeft(s []int, nint) {
iflen(s) ==0||n%len(s) ==0 {
return
}
n=n%len(s)
ss:=make([]int, len(s))
copy(ss, s)
copy(s, ss[n:])
copy(s[len(s)-n:], ss[0:n])
}
Generic solution that doesn't perform in place rotation.
// Rotates the slice s to the left by n stepsfuncrotateLeft(s []int, nint) []int {
iflen(s) ==0||n%len(s) ==0 {
returns
}
n=n%len(s)
s=append(s[n:], s[0:n]...)
returns
}
The text was updated successfully, but these errors were encountered:
The exercise requested to write a version of rotate that operates in a single pass. The answer provided rotates a slice by one position to the left, so while your code runs correctly it's not the right answer to the exercise.
Generic solution that performs in place rotation
Generic solution that doesn't perform in place rotation.
The text was updated successfully, but these errors were encountered: