我無法讓這個 Go lang 測試程序運行。編譯器在下面的 append() 函數調用中不斷給出錯誤,并顯示“已評估但未使用”錯誤。我不知道為什么。package mainimport ( "fmt")func removeDuplicates(testArr *[]int) int { prevValue := (*testArr)[0] for curIndex := 1; curIndex < len((*testArr)); curIndex++ { curValue := (*testArr)[curIndex] if curValue == prevValue { append((*testArr)[:curIndex], (*testArr)[curIndex+1:]...) } prevValue = curValue } return len(*testArr)}func main() { testArr := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4} nonDupSize := removeDuplicates(&testArr) fmt.Printf("nonDupSize = %d", nonDupSize)}
2 回答

qq_遁去的一_1
TA貢獻1725條經驗 獲得超8個贊
“已評估但未使用”錯誤。
下面的代碼是我的想法。我認為你的代碼不是很清楚。
package main
import (
? ? "fmt"
)
func removeDuplicates(testArr *[]int) int {
? ? m := make(map[int]bool)
? ? arr := make([]int, 0)
? ? for curIndex := 0; curIndex < len((*testArr)); curIndex++ {
? ? ? ? curValue := (*testArr)[curIndex]
? ? ? ? if has :=m[curValue]; !has {
? ? ? ? ? ? m[curValue] = true
? ? ? ? ? ? arr = append(arr, curValue)
? ? ? ? }
? ? }
? ? *testArr = arr
? ? return len(*testArr)
}
func main() {
? ? testArr := []int{0, 0, 1, 1, 1, 2, 2, 3, 3, 4}
? ? nonDupSize := removeDuplicates(&testArr)
? ? fmt.Printf("nonDupSize = %d", nonDupSize)
}
- 2 回答
- 0 關注
- 138 瀏覽
添加回答
舉報
0/150
提交
取消