golang版本 package main import ( "fmt" ) func main() { ss := []string{"word", "dd", "da", "dc", "dword", "d"} fmt.Println(jielong(ss, 0)) } func jielong(ss []string, firstIdx int) string { first := ss[firstIdx] idx := firstIdx ss = append(ss[:idx], ss[idx+1:]...) res := first for { first, idx = find(ss, first[len(first)-1]) if first != "" { res += first ss = append(ss[:idx], ss[idx+1:]...) } else { break } } return res } func find(ss []string, first byte) (string, int) { best := "" idx := -1 for i := 0; i < len(ss); i++ { if ss[i][0] == first { if best == "" { best = ss[i] } else { if len(ss[i]) == len(best) &;&; ss[i] < best { best = ss[i] idx = i } else if len(ss[i]) > len(best) { best = ss[i] idx = i } } } } return best, idx }
点赞 评论

相关推荐

牛客网
牛客企业服务