|
42 | 42 |
|
43 | 43 | <!-- 这里可写通用的实现逻辑 -->
|
44 | 44 |
|
| 45 | +**方法一:哈希表** |
| 46 | + |
| 47 | +用哈希表存放所有单词。遍历这些单词,找出**长度最长且字典序最小**的单词。 |
| 48 | + |
| 49 | +**方法二:排序** |
| 50 | + |
45 | 51 | 优先返回符合条件、**长度最长且字典序最小**的单词,那么可以进行依照该规则,先对 `words` 进行排序,免去多个结果之间的比较。
|
46 | 52 |
|
47 | 53 | <!-- tabs:start -->
|
|
51 | 57 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
52 | 58 |
|
53 | 59 | ```python
|
54 |
| - |
| 60 | +class Solution: |
| 61 | + def longestWord(self, words: List[str]) -> str: |
| 62 | + cnt, ans = 0, '' |
| 63 | + s = set(words) |
| 64 | + for w in s: |
| 65 | + n = len(w) |
| 66 | + if all(w[:i] in s for i in range(1, n)): |
| 67 | + if cnt < n: |
| 68 | + cnt, ans = n, w |
| 69 | + elif cnt == n and w < ans: |
| 70 | + ans = w |
| 71 | + return ans |
55 | 72 | ```
|
56 | 73 |
|
57 | 74 | ### **Java**
|
58 | 75 |
|
59 | 76 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
60 | 77 |
|
61 | 78 | ```java
|
| 79 | +class Solution { |
| 80 | + private Set<String> s; |
| 81 | + |
| 82 | + public String longestWord(String[] words) { |
| 83 | + s = new HashSet<>(Arrays.asList(words)); |
| 84 | + int cnt = 0; |
| 85 | + String ans = ""; |
| 86 | + for (String w : s) { |
| 87 | + int n = w.length(); |
| 88 | + if (check(w)) { |
| 89 | + if (cnt < n) { |
| 90 | + cnt = n; |
| 91 | + ans = w; |
| 92 | + } else if (cnt == n && w.compareTo(ans) < 0) { |
| 93 | + ans = w; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + return ans; |
| 98 | + } |
62 | 99 |
|
| 100 | + private boolean check(String word) { |
| 101 | + for (int i = 1, n = word.length(); i < n; ++i) { |
| 102 | + if (!s.contains(word.substring(0, i))) { |
| 103 | + return false; |
| 104 | + } |
| 105 | + } |
| 106 | + return true; |
| 107 | + } |
| 108 | +} |
63 | 109 | ```
|
64 | 110 |
|
65 | 111 | ### **TypeScript**
|
@@ -113,6 +159,72 @@ impl Solution {
|
113 | 159 | }
|
114 | 160 | ```
|
115 | 161 |
|
| 162 | +### **C++** |
| 163 | + |
| 164 | +```cpp |
| 165 | +class Solution { |
| 166 | +public: |
| 167 | + string longestWord(vector<string>& words) { |
| 168 | + unordered_set<string> s(words.begin(), words.end()); |
| 169 | + int cnt = 0; |
| 170 | + string ans = ""; |
| 171 | + for (auto w : s) |
| 172 | + { |
| 173 | + int n = w.size(); |
| 174 | + if (check(w, s)) |
| 175 | + { |
| 176 | + if (cnt < n) |
| 177 | + { |
| 178 | + cnt = n; |
| 179 | + ans = w; |
| 180 | + } |
| 181 | + else if (cnt == n && w < ans) ans = w; |
| 182 | + } |
| 183 | + } |
| 184 | + return ans; |
| 185 | + } |
| 186 | + |
| 187 | + bool check(string& word, unordered_set<string>& s) { |
| 188 | + for (int i = 1, n = word.size(); i < n; ++i) |
| 189 | + if (!s.count(word.substr(0, i))) |
| 190 | + return false; |
| 191 | + return true; |
| 192 | + } |
| 193 | +}; |
| 194 | +``` |
| 195 | + |
| 196 | +### **Go** |
| 197 | + |
| 198 | +```go |
| 199 | +func longestWord(words []string) string { |
| 200 | + s := make(map[string]bool) |
| 201 | + for _, w := range words { |
| 202 | + s[w] = true |
| 203 | + } |
| 204 | + cnt := 0 |
| 205 | + ans := "" |
| 206 | + check := func(word string) bool { |
| 207 | + for i, n := 1, len(word); i < n; i++ { |
| 208 | + if !s[word[:i]] { |
| 209 | + return false |
| 210 | + } |
| 211 | + } |
| 212 | + return true |
| 213 | + } |
| 214 | + for w, _ := range s { |
| 215 | + n := len(w) |
| 216 | + if check(w) { |
| 217 | + if cnt < n { |
| 218 | + cnt, ans = n, w |
| 219 | + } else if cnt == n && w < ans { |
| 220 | + ans = w |
| 221 | + } |
| 222 | + } |
| 223 | + } |
| 224 | + return ans |
| 225 | +} |
| 226 | +``` |
| 227 | + |
116 | 228 | ### **...**
|
117 | 229 |
|
118 | 230 | ```
|
|
0 commit comments