标签:ever pre 时间 int UNC func reverse fun print
反转func reverse(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func main() {
a := [...]int{0, 1, 2, 3, 4, 5}
reverse(a[:])
fmt.Println(a)
}
数组反转
func reverse(s *[7]int) {
for i, j := 0, len(*s)-1; i < j; i, j = i+1, j-1 {
(*s)[i], (*s)[j] = (*s)[j], (*s)[i]
}
}
func main() {
arr := [7]int{0, 1, 2, 3, 4, 5, 6}
reverse(&arr)
fmt.Println(arr)
}
标签:ever pre 时间 int UNC func reverse fun print
原文地址:https://blog.51cto.com/13766835/2432782