Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

最后的数组的相对位置是变了啊 #3

Open
siyalight opened this issue Jun 10, 2020 · 1 comment
Open

最后的数组的相对位置是变了啊 #3

siyalight opened this issue Jun 10, 2020 · 1 comment

Comments

@siyalight
Copy link

@songjiayang
Copy link

确实变了,大概修改了一下:

package main

import (
	"fmt"
)

func main() {
	items := []int{1, 2, 3, 4}
	oddFirst(items)
	fmt.Println(items)
}

func oddFirst(s []int) {
	if len(s) < 2 {
		return
	}

	i, j := len(s)-2, len(s)-1
	for i >= 0 {
		if s[j]%2 == 0 {
			j--
		} else if s[i]%2 == 0 {
			temp := s[i]
			copy(s[i:j], s[i+1:j+1])
			s[j] = temp
			j--
			i--
		} else {
			i--
		}
	}
}

= >

[1 3 2 4]

Repository owner deleted a comment Feb 2, 2024
@github-staff github-staff deleted a comment Feb 21, 2024
Repository owner deleted a comment from fede-s Feb 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@songjiayang @siyalight and others