Clear2 / Algorithm

算法训练
MIT License
0 stars 0 forks source link

最长公共子串 #3

Open Clear2 opened 4 years ago

Clear2 commented 4 years ago
func longestCommonPrefix(strs []string) string {
    if len(strs) == 0 {
        return ""
    }
    if len(strs) == 1 {
        return strs[0]
    }
    prefix := strs[0]

    for _, k := range strs {
        fmt.Println(k)

        for strings.Index(k, prefix) != 0 {
            if len(prefix) == 0 {
                return ""
            }
            prefix = prefix[:len(prefix) -1]
        }
    }
    return prefix
}