版权声明:原创勿转 https://cloud.tencent.com/developer/article/1419042
先排序
func longestWord(words []string) string {
sort.Strings(words)
m := make(map[string]bool)
ret := ""
for _, w := range words {
if len(w) == 1 || m[w[0:len(w)-1]] == true {
if len(ret) < len(w) {
ret = w
}
m[w] = true
}
}
return ret
}
更多内容请移步我的repo:https://github.com/anakin/golang-leetcode