mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +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]
|
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
|
```go
|
||||||
// moveToFront moves needle to the front of haystack, in place if possible.
|
// moveToFront moves needle to the front of haystack, in place if possible.
|
||||||
func moveToFront(needle string, haystack []string) []string {
|
func moveToFront(needle string, haystack []string) []string {
|
||||||
if len(haystack) == 0 || haystack[0] == needle {
|
if len(haystack) != 0 && haystack[0] == needle {
|
||||||
return haystack
|
return haystack
|
||||||
}
|
}
|
||||||
var prev string
|
prev := needle
|
||||||
for i, elem := range haystack {
|
for i, elem := range haystack {
|
||||||
switch {
|
switch {
|
||||||
case i == 0:
|
case i == 0:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user