题目描述
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 “”。
解题代码
func longestCommonPrefix(strs []string) string { if strs[0] == "" { return "" } index := 1 s := "" n := len(strs[0]) for index <= n { s = strs[0][:index] for _, s2 := range strs { if index <= len(s2) { if s2[:index] != s { return s[:len(s) - 1] } } else { return s[:len(s) - 1] } } index ++ } return "" }
提交结果