Skip to content

Instantly share code, notes, and snippets.

@afrikaan-official
Created July 3, 2017 13:04
Show Gist options
  • Save afrikaan-official/ded3191caaa475ec2d67f2a6202fad51 to your computer and use it in GitHub Desktop.
Save afrikaan-official/ded3191caaa475ec2d67f2a6202fad51 to your computer and use it in GitHub Desktop.
reverse an array without using a temp variable
package main
import "fmt"
func main() {
v := []int{
1,
2,
3,
4,
5,
6,
10,
}
reverse(v)
}
func reverse(d []int) {
l := len(d)
for i := 0; i < (l+1)/2; i++ {
sum := 0
if i != l-1-i {
sum = d[i] + d[l-1-i]
d[i], d[l-1-i] = sum-d[i], sum-d[l-1-i]
} else {
sum = d[i]
}
}
fmt.Println(d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment