mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
@ -0,0 +1,23 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func longestCommonPrefix(strs []string) string {
|
||||
sort.Slice(strs, func(i, j int) bool {
|
||||
return len(strs[i]) <= len(strs[j])
|
||||
})
|
||||
minLen := len(strs[0])
|
||||
if minLen == 0 {
|
||||
return ""
|
||||
}
|
||||
var commonPrefix []byte
|
||||
for i := 0; i < minLen; i++ {
|
||||
for j := 1; j < len(strs); j++ {
|
||||
if strs[j][i] != strs[0][i] {
|
||||
return string(commonPrefix)
|
||||
}
|
||||
}
|
||||
commonPrefix = append(commonPrefix, strs[0][i])
|
||||
}
|
||||
return string(commonPrefix)
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question14 struct {
|
||||
para14
|
||||
ans14
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
type para14 struct {
|
||||
strs []string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
type ans14 struct {
|
||||
ans string
|
||||
}
|
||||
|
||||
func Test_Problem14(t *testing.T) {
|
||||
|
||||
qs := []question14{
|
||||
|
||||
{
|
||||
para14{[]string{"flower", "flow", "flight"}},
|
||||
ans14{"fl"},
|
||||
},
|
||||
|
||||
{
|
||||
para14{[]string{"dog", "racecar", "car"}},
|
||||
ans14{""},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 14------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans14, q.para14
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p.strs, longestCommonPrefix(p.strs))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
63
leetcode/0014.Longest-Common-Prefix/README.md
Normal file
63
leetcode/0014.Longest-Common-Prefix/README.md
Normal file
@ -0,0 +1,63 @@
|
||||
# [14. Longest Common Prefix](https://leetcode-cn.com/problems/longest-common-prefix/)
|
||||
|
||||
## 题目
|
||||
|
||||
Write a function to find the longest common prefix string amongst an array of strings.
|
||||
|
||||
If there is no common prefix, return an empty string "".
|
||||
|
||||
**Example 1**:
|
||||
|
||||
Input: strs = ["flower","flow","flight"]
|
||||
Output: "fl"
|
||||
|
||||
**Example 2**:
|
||||
|
||||
Input: strs = ["dog","racecar","car"]
|
||||
Output: ""
|
||||
Explanation: There is no common prefix among the input strings.
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- 1 <= strs.length <= 200
|
||||
- 0 <= strs[i].length <= 200
|
||||
- strs[i] consists of only lower-case English letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
编写一个函数来查找字符串数组中的最长公共前缀。
|
||||
|
||||
如果不存在公共前缀,返回空字符串 ""。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 对strs按照字符串长度进行升序排序,求出strs中长度最小字符串的长度minLen
|
||||
- 逐个比较长度最小字符串与其它字符串中的字符,如果不相等就返回commonPrefix,否则就把该字符加入commonPrefix
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
func longestCommonPrefix(strs []string) string {
|
||||
sort.Slice(strs, func(i, j int) bool {
|
||||
return len(strs[i]) <= len(strs[j])
|
||||
})
|
||||
minLen := len(strs[0])
|
||||
if minLen == 0 {
|
||||
return ""
|
||||
}
|
||||
var commonPrefix []byte
|
||||
for i := 0; i < minLen; i++ {
|
||||
for j := 1; j < len(strs); j++ {
|
||||
if strs[j][i] != strs[0][i] {
|
||||
return string(commonPrefix)
|
||||
}
|
||||
}
|
||||
commonPrefix = append(commonPrefix, strs[0][i])
|
||||
}
|
||||
return string(commonPrefix)
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user