问题如题。
我最近在看 effective go 文档,看到下面里的代码。这令我百思不得其解,为什么是 O(N^2) ?
难道是字符串拼接要 O(N) 吗?
```go
// Method for printing - sorts the elements before printing.
func (s Sequence) String() string {
s = s.Copy() // Make a copy; don't overwrite argument.
sort.Sort(s)
str := "["
for i, elem := range s { // Loop is O(N²); will fix that in next example.
if i > 0 {
str += " "
}
str += fmt.Sprint(elem)
}
return str + "]"
}
```