Skip to content

Commit ed6a485

Browse files
committed
Create longest-common-prefix.go
1 parent 22ff9e0 commit ed6a485

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

longest-common-prefix.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode_solutions_golang
2+
3+
//https://leetcode.com/problems/longest-common-prefix/
4+
func longestCommonPrefix(strs []string) string {
5+
shortest := 2<<31 - 1
6+
for _, str := range strs {
7+
if len(str) < shortest {
8+
shortest = len(str)
9+
}
10+
}
11+
result := ""
12+
for i := 0; i < shortest; i++ {
13+
for j := 1; j < len(strs); j++ {
14+
if strs[j][i] != strs[j-1][i] {
15+
return result
16+
}
17+
}
18+
result += string(strs[0][i])
19+
}
20+
return result
21+
}

0 commit comments

Comments
 (0)