Skip to content

Commit 7c80298

Browse files
authored
Merge pull request halfrost#192 from gostool/leetcode0014
Leetcode0014
2 parents 0495d2b + 1ba28c5 commit 7c80298

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
import "sort"
4+
5+
func longestCommonPrefix(strs []string) string {
6+
sort.Slice(strs, func(i, j int) bool {
7+
return len(strs[i]) <= len(strs[j])
8+
})
9+
minLen := len(strs[0])
10+
if minLen == 0 {
11+
return ""
12+
}
13+
var commonPrefix []byte
14+
for i := 0; i < minLen; i++ {
15+
for j := 1; j < len(strs); j++ {
16+
if strs[j][i] != strs[0][i] {
17+
return string(commonPrefix)
18+
}
19+
}
20+
commonPrefix = append(commonPrefix, strs[0][i])
21+
}
22+
return string(commonPrefix)
23+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question14 struct {
9+
para14
10+
ans14
11+
}
12+
13+
// para 是参数
14+
type para14 struct {
15+
strs []string
16+
}
17+
18+
// ans 是答案
19+
type ans14 struct {
20+
ans string
21+
}
22+
23+
func Test_Problem14(t *testing.T) {
24+
25+
qs := []question14{
26+
27+
{
28+
para14{[]string{"flower", "flow", "flight"}},
29+
ans14{"fl"},
30+
},
31+
32+
{
33+
para14{[]string{"dog", "racecar", "car"}},
34+
ans14{""},
35+
},
36+
}
37+
38+
fmt.Printf("------------------------Leetcode Problem 14------------------------\n")
39+
40+
for _, q := range qs {
41+
_, p := q.ans14, q.para14
42+
fmt.Printf("【input】:%v 【output】:%v\n", p.strs, longestCommonPrefix(p.strs))
43+
}
44+
fmt.Printf("\n\n\n")
45+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# [14. Longest Common Prefix](https://leetcode-cn.com/problems/longest-common-prefix/)
2+
3+
## 题目
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string "".
8+
9+
**Example 1**:
10+
11+
Input: strs = ["flower","flow","flight"]
12+
Output: "fl"
13+
14+
**Example 2**:
15+
16+
Input: strs = ["dog","racecar","car"]
17+
Output: ""
18+
Explanation: There is no common prefix among the input strings.
19+
20+
**Constraints:**
21+
22+
- 1 <= strs.length <= 200
23+
- 0 <= strs[i].length <= 200
24+
- strs[i] consists of only lower-case English letters.
25+
26+
## 题目大意
27+
28+
编写一个函数来查找字符串数组中的最长公共前缀。
29+
30+
如果不存在公共前缀,返回空字符串 ""。
31+
32+
## 解题思路
33+
34+
- 对strs按照字符串长度进行升序排序,求出strs中长度最小字符串的长度minLen
35+
- 逐个比较长度最小字符串与其它字符串中的字符,如果不相等就返回commonPrefix,否则就把该字符加入commonPrefix
36+
37+
## 代码
38+
39+
```go
40+
package leetcode
41+
42+
import "sort"
43+
44+
func longestCommonPrefix(strs []string) string {
45+
sort.Slice(strs, func(i, j int) bool {
46+
return len(strs[i]) <= len(strs[j])
47+
})
48+
minLen := len(strs[0])
49+
if minLen == 0 {
50+
return ""
51+
}
52+
var commonPrefix []byte
53+
for i := 0; i < minLen; i++ {
54+
for j := 1; j < len(strs); j++ {
55+
if strs[j][i] != strs[0][i] {
56+
return string(commonPrefix)
57+
}
58+
}
59+
commonPrefix = append(commonPrefix, strs[0][i])
60+
}
61+
return string(commonPrefix)
62+
}
63+
```

0 commit comments

Comments
 (0)