mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
规范格式
This commit is contained in:
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"sort"
|
||||
)
|
||||
|
||||
func longestWord(words []string) string {
|
||||
sort.Strings(words)
|
||||
mp := make(map[string]bool)
|
||||
var res string
|
||||
for _, word := range words {
|
||||
size := len(word)
|
||||
if size == 1 || mp[word[:size-1]] {
|
||||
if size > len(res) {
|
||||
res = word
|
||||
}
|
||||
mp[word] = true
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question720 struct {
|
||||
para720
|
||||
ans720
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para720 struct {
|
||||
w []string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans720 struct {
|
||||
one string
|
||||
}
|
||||
|
||||
func Test_Problem720(t *testing.T) {
|
||||
|
||||
qs := []question720{
|
||||
|
||||
question720{
|
||||
para720{[]string{"w", "wo", "wor", "worl", "world"}},
|
||||
ans720{"world"},
|
||||
},
|
||||
|
||||
question720{
|
||||
para720{[]string{"a", "banana", "app", "appl", "ap", "apply", "apple"}},
|
||||
ans720{"apple"},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 720------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans720, q.para720
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, longestWord(p.w))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
43
leetcode/0720.Longest-Word-in-Dictionary/README.md
Executable file
43
leetcode/0720.Longest-Word-in-Dictionary/README.md
Executable file
@ -0,0 +1,43 @@
|
||||
# [720. Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a list of strings `words` representing an English Dictionary, find the longest word in `words` that can be built one character at a time by other words in `words`. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
|
||||
|
||||
If there is no answer, return the empty string.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input:
|
||||
words = ["w","wo","wor","worl", "world"]
|
||||
Output: "world"
|
||||
Explanation:
|
||||
The word "world" can be built one character at a time by "w", "wo", "wor", and "worl".
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input:
|
||||
words = ["a", "banana", "app", "appl", "ap", "apply", "apple"]
|
||||
Output: "apple"
|
||||
Explanation:
|
||||
Both "apply" and "apple" can be built from other words in the dictionary. However, "apple" is lexicographically smaller than "apply".
|
||||
|
||||
**Note:**
|
||||
|
||||
- All the strings in the input will only contain lowercase letters.
|
||||
- The length of `words` will be in the range `[1, 1000]`.
|
||||
- The length of `words[i]` will be in the range `[1, 30]`.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
给出一个字符串数组 words 组成的一本英语词典。从中找出最长的一个单词,该单词是由 words 词典中其他单词逐步添加一个字母组成。若其中有多个可行的答案,则返回答案中字典序最小的单词。若无答案,则返回空字符串。
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 给出一个字符串数组,要求找到长度最长的,并且可以由字符串数组里面其他字符串拼接一个字符组成的字符串。如果存在多个这样的最长的字符串,则输出字典序较小的那个字符串,如果找不到这样的字符串,输出空字符串。
|
||||
- 这道题解题思路是先排序,排序完成以后就是字典序从小到大了。之后再用 map 辅助记录即可。
|
Reference in New Issue
Block a user