|
| 1 | +--- |
| 2 | +title: 2942.查找包含给定字符的单词:使用库函数完成 |
| 3 | +date: 2025-05-24 21:41:41 |
| 4 | +tags: [题解, LeetCode, 简单, 数组, 字符串] |
| 5 | +categories: [题解, LeetCode] |
| 6 | +--- |
| 7 | + |
| 8 | +# 【LetMeFly】2942.查找包含给定字符的单词:使用库函数完成 |
| 9 | + |
| 10 | +力扣题目链接:[https://leetcode.cn/problems/find-words-containing-character/](https://leetcode.cn/problems/find-words-containing-character/) |
| 11 | + |
| 12 | +<p>给你一个下标从 <strong>0</strong> 开始的字符串数组 <code>words</code> 和一个字符 <code>x</code> 。</p> |
| 13 | + |
| 14 | +<p>请你返回一个 <strong>下标数组</strong> ,表示下标在数组中对应的单词包含字符 <code>x</code> 。</p> |
| 15 | + |
| 16 | +<p><b>注意</b> ,返回的数组可以是 <strong>任意</strong> 顺序。</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | + |
| 20 | +<p><strong class="example">示例 1:</strong></p> |
| 21 | + |
| 22 | +<pre> |
| 23 | +<b>输入:</b>words = ["leet","code"], x = "e" |
| 24 | +<b>输出:</b>[0,1] |
| 25 | +<b>解释:</b>"e" 在两个单词中都出现了:"l<em><strong>ee</strong></em>t" 和 "cod<em><strong>e</strong></em>" 。所以我们返回下标 0 和 1 。 |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong class="example">示例 2:</strong></p> |
| 29 | + |
| 30 | +<pre> |
| 31 | +<b>输入:</b>words = ["abc","bcd","aaaa","cbc"], x = "a" |
| 32 | +<b>输出:</b>[0,2] |
| 33 | +<b>解释:</b>"a" 在 "<em><strong>a</strong></em>bc" 和 "<em><strong>aaaa</strong></em>" 中出现了,所以我们返回下标 0 和 2 。 |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p><strong class="example">示例 3:</strong></p> |
| 37 | + |
| 38 | +<pre> |
| 39 | +<b>输入:</b>words = ["abc","bcd","aaaa","cbc"], x = "z" |
| 40 | +<b>输出:</b>[] |
| 41 | +<b>解释:</b>"z" 没有在任何单词中出现。所以我们返回空数组。 |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | + |
| 46 | +<p><strong>提示:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li><code>1 <= words.length <= 50</code></li> |
| 50 | + <li><code>1 <= words[i].length <= 50</code></li> |
| 51 | + <li><code>x</code> 是一个小写英文字母。</li> |
| 52 | + <li><code>words[i]</code> 只包含小写英文字母。</li> |
| 53 | +</ul> |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## 解题方法:模拟 |
| 58 | + |
| 59 | +遍历字符串数组中的每个字符串,如果字符串中包含字符`x`,则将字符串对应下标添加到答案数组中。 |
| 60 | + |
| 61 | ++ 时间复杂度$O(\sum)$,其中$\sum$是字符数之和 |
| 62 | ++ 空间复杂度$O(1)$,力扣函数返回值不计入空间复杂度 |
| 63 | + |
| 64 | +### AC代码 |
| 65 | + |
| 66 | +#### C++ |
| 67 | + |
| 68 | +```cpp |
| 69 | +/* |
| 70 | + * @Author: LetMeFly |
| 71 | + * @Date: 2025-05-24 21:30:36 |
| 72 | + * @LastEditors: LetMeFly.xyz |
| 73 | + * @LastEditTime: 2025-05-24 21:34:02 |
| 74 | + */ |
| 75 | +class Solution { |
| 76 | +public: |
| 77 | + vector<int> findWordsContaining(vector<string>& words, char x) { |
| 78 | + vector<int> ans; |
| 79 | + for (int i = 0; i < words.size(); i++) { |
| 80 | + if (words[i].find(x) != string::npos) { |
| 81 | + ans.push_back(i); |
| 82 | + } |
| 83 | + } |
| 84 | + return ans; |
| 85 | + } |
| 86 | +}; |
| 87 | +``` |
| 88 | +
|
| 89 | +#### Python |
| 90 | +
|
| 91 | +```python |
| 92 | +''' |
| 93 | +Author: LetMeFly |
| 94 | +Date: 2025-05-24 21:30:36 |
| 95 | +LastEditors: LetMeFly.xyz |
| 96 | +LastEditTime: 2025-05-24 21:37:05 |
| 97 | +''' |
| 98 | +from typing import List |
| 99 | +
|
| 100 | +class Solution: |
| 101 | + def findWordsContaining(self, words: List[str], x: str) -> List[int]: |
| 102 | + return [i for i in range(len(words)) if x in words[i]] |
| 103 | +``` |
| 104 | + |
| 105 | +#### Java |
| 106 | + |
| 107 | +```java |
| 108 | +/* |
| 109 | + * @Author: LetMeFly |
| 110 | + * @Date: 2025-05-24 21:30:36 |
| 111 | + * @LastEditors: LetMeFly.xyz |
| 112 | + * @LastEditTime: 2025-05-24 21:38:47 |
| 113 | + */ |
| 114 | +import java.util.List; |
| 115 | +import java.util.ArrayList; |
| 116 | + |
| 117 | +class Solution { |
| 118 | + public List<Integer> findWordsContaining(String[] words, char x) { |
| 119 | + List<Integer> ans = new ArrayList<>(); |
| 120 | + for (int i = 0; i < words.length; i++) { |
| 121 | + if (words[i].indexOf(x) >= 0) { |
| 122 | + ans.add(i); |
| 123 | + } |
| 124 | + } |
| 125 | + return ans; |
| 126 | + } |
| 127 | +} |
| 128 | +``` |
| 129 | + |
| 130 | +#### Go |
| 131 | + |
| 132 | +```go |
| 133 | +/* |
| 134 | + * @Author: LetMeFly |
| 135 | + * @Date: 2025-05-24 21:30:36 |
| 136 | + * @LastEditors: LetMeFly.xyz |
| 137 | + * @LastEditTime: 2025-05-24 21:40:52 |
| 138 | + */ |
| 139 | +package main |
| 140 | + |
| 141 | +import "strings" |
| 142 | + |
| 143 | +func findWordsContaining(words []string, x byte) (ans []int) { |
| 144 | + for i, word := range words { |
| 145 | + if strings.IndexByte(word, x) >= 0 { |
| 146 | + ans = append(ans, i) |
| 147 | + } |
| 148 | + } |
| 149 | + return |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/148197875)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/05/24/LeetCode%202942.%E6%9F%A5%E6%89%BE%E5%8C%85%E5%90%AB%E7%BB%99%E5%AE%9A%E5%AD%97%E7%AC%A6%E7%9A%84%E5%8D%95%E8%AF%8D/)哦~ |
| 154 | +> |
| 155 | +> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode) |
0 commit comments