-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlongest_common_prefix.go
More file actions
67 lines (52 loc) · 1.14 KB
/
longest_common_prefix.go
File metadata and controls
67 lines (52 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
14. 最长公共前缀
字符串
简单
编写一个函数来查找字符串数组中的最长公共前缀。
如果不存在公共前缀,返回空字符串 ""。
示例 1:
输入:strs = ["flower","flow","flight"]
输出:"fl"
示例 2:
输入:strs = ["dog","racecar","car"]
输出:""
解释:输入不存在公共前缀。
提示:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] 仅由小写英文字母组成
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-common-prefix
*/
package main
import (
"leetcode/go/utils"
)
func longestCommonPrefix(strs []string) string {
sSize := len(strs)
minSize := 200
for _, s := range strs {
size := len(s)
if size < minSize {
minSize = size
}
}
stop := false
index := -1
for i := 0; i < minSize; i++ {
for j := 0; j < sSize-1; j++ {
if strs[j][i] != strs[j+1][i] {
stop = true
}
}
if stop {
break
}
index = i
}
return strs[0][:index+1]
}
func main() {
utils.Assert(longestCommonPrefix([]string{"flower", "flow", "flight"}), "fl")
utils.Assert(longestCommonPrefix([]string{"dog", "racecar", "car"}), "")
}