mirror of
https://github.com/golang/go.git
synced 2025-05-05 15:43:04 +00:00
Fix moveToFront and its headline
parent
4c2589e614
commit
bffe6342c2
@ -234,15 +234,15 @@ result := in[:j+1]
|
||||
fmt.Println(result) // [1 2 3 4]
|
||||
```
|
||||
|
||||
### Move to front, or append if not present, in place
|
||||
### Move to front, or prepend if not present, in place if possible.
|
||||
|
||||
```go
|
||||
// moveToFront moves needle to the front of haystack, in place if possible.
|
||||
func moveToFront(needle string, haystack []string) []string {
|
||||
if len(haystack) == 0 || haystack[0] == needle {
|
||||
if len(haystack) != 0 && haystack[0] == needle {
|
||||
return haystack
|
||||
}
|
||||
var prev string
|
||||
prev := needle
|
||||
for i, elem := range haystack {
|
||||
switch {
|
||||
case i == 0:
|
||||
|
Loading…
x
Reference in New Issue
Block a user