猿问

删除字符串切片中的相邻重复项

我有一个问题陈述write an in-place function to eliminate the adjacent duplicates in a string slice.


我想出了以下代码


func main() {

    tempData := []string{"abc", "abc", "abc", "def", "def", "ghi"}

    removeAdjacentDuplicates(tempData)

    fmt.Println(tempData)

}


func removeAdjacentDuplicates(data []string) {

    for j := 1; j < len(data); {

        if data[j-1] == data[j] {

            data = append(data[:j], data[j+1:]...)

        } else {

            j++

        }

    }

    fmt.Println(data)

}

输出如下


[abc def ghi]

[abc def ghi ghi ghi ghi]

我的疑问是,如果在函数中,切片被修改,那么在调用函数中,为什么切片没有给出正确的结果?此外,任何更好地理解(和底层)的文章都将非常有帮助。slicesarray


qq_花开花谢_0
浏览 100回答 3
3回答

牧羊人nacy

func removeAdjacentDuplicate 将切片“就好像”它是对 tempData 的引用一样main() 中 tempData 的容量和长度在程序的生命周期内保持不变在 removeAdjacentDuplicate func 中,每次找到一个 dupe 时,“ghi”的最终值就会从末尾移动到末尾 - 1。因此,在切片末尾的记忆中,有重复的“ghi”当控件返回到 main 时,程序将打印出现在已修改的切片 tempData。因为它是以类似于对函数的引用的方式传递的,所以修改的是此内存。函数调用未创建内存的副本您可以通过在程序运行时查看 cap() 和 len() 来查看此行为package mainimport (&nbsp; &nbsp; &nbsp; &nbsp; "fmt")func main() {&nbsp; &nbsp; &nbsp; &nbsp; tempData := []string{"abc", "abc", "abc", "def", "def", "ghi"}&nbsp; &nbsp; &nbsp; &nbsp; removeAdjacentDuplicates(tempData)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(tempData,cap(tempData),len(tempData))}func removeAdjacentDuplicates(data []string) {&nbsp; &nbsp; &nbsp; &nbsp; for j := 1; j < len(data); {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if data[j-1] == data[j] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = append(data[:j], data[j+1:]...)&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(data,cap(data),len(data))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(data, cap(data),len(data))}

慕森王

在代码中,想要改变在参数中传递的 slcie。这实际上是不可能的。removeAdjacentDuplicates此函数应返回新切片,就像返回一样。appendfunc removeAdjacentDuplicates(data []string) []string{&nbsp; &nbsp; for j := 1; j < len(data); {&nbsp; &nbsp; &nbsp; &nbsp; if data[j-1] == data[j] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = append(data[:j], data[j+1:]...)&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j++&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return data}如果您确实想改变参数,这是可能的,但您需要传递指向切片的指针*[]string

慕田峪7331174

试试这个功能:func deleteAdjacentDuplicate(slice []string) []string {&nbsp; &nbsp; for i := 1; i < len(slice); i++ {&nbsp; &nbsp; &nbsp; &nbsp; if slice[i-1] == slice[i] {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; copy(slice[i:], slice[i+1:]) //copy [4] where there is [3, 4] => [4, 4]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slice = slice[:len(slice)-1] //removes last element&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i-- //avoid advancing counter&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return slice}
随时随地看视频慕课网APP

相关分类

Go
我要回答