Created
July 3, 2017 13:04
-
-
Save afrikaan-official/ded3191caaa475ec2d67f2a6202fad51 to your computer and use it in GitHub Desktop.
reverse an array without using a temp variable
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
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