Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6b0395e54a |
11
reverse.go
11
reverse.go
@ -1,6 +1,15 @@
|
|||||||
package slices
|
package slices
|
||||||
|
|
||||||
func Reverse[S ~[]T, T any](s S) {
|
func Reverse[T any](slice []T) []T {
|
||||||
|
s := make([]T, len(slice))
|
||||||
|
for i := range slice {
|
||||||
|
ri := len(slice) - 1 - i
|
||||||
|
s[ri] = slice[i]
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReverseInPlace[S ~[]T, T any](s S) {
|
||||||
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
|
||||||
s[i], s[j] = s[j], s[i]
|
s[i], s[j] = s[j], s[i]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user