Skip to content

Commit 8541171

Browse files
authored
update: 添加问题“2942.查找包含给定字符的单词”的代码和题解(#953)
* 2941: half.cpp (#952) Signed-off-by: LetMeFly666 <[email protected]> * 2941: AC.cpp+RE.py (#952) Signed-off-by: LetMeFly666 <[email protected]> * 2941: AC.py+AC.java(AC,100.00%,88.61%)+AC.go(AC,100.00%,16.67%) (#952) Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2942.查找包含给定字符的单词”的代码和题解(#953) Signed-off-by: LetMeFly666 <[email protected]> * rollback: clean (#952) Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 256fa92 commit 8541171

9 files changed

+229
-28
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-24 21:30:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-24 21:34:02
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
public:
13+
vector<int> findWordsContaining(vector<string>& words, char x) {
14+
vector<int> ans;
15+
for (int i = 0; i < words.size(); i++) {
16+
if (words[i].find(x) != string::npos) {
17+
ans.push_back(i);
18+
}
19+
}
20+
return ans;
21+
}
22+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-24 21:30:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-24 21:40:52
6+
*/
7+
package main
8+
9+
import "strings"
10+
11+
func findWordsContaining(words []string, x byte) (ans []int) {
12+
for i, word := range words {
13+
if strings.IndexByte(word, x) >= 0 {
14+
ans = append(ans, i)
15+
}
16+
}
17+
return
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-05-24 21:30:36
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-05-24 21:38:47
6+
*/
7+
import java.util.List;
8+
import java.util.ArrayList;
9+
10+
class Solution {
11+
public List<Integer> findWordsContaining(String[] words, char x) {
12+
List<Integer> ans = new ArrayList<>();
13+
for (int i = 0; i < words.length; i++) {
14+
if (words[i].indexOf(x) >= 0) {
15+
ans.add(i);
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-05-24 21:30:36
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-05-24 21:37:05
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
11+
return [i for i in range(len(words)) if x in words[i]]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@
877877
|2928.给小朋友们分糖果I|简单|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/01/LeetCode%202928.%E7%BB%99%E5%B0%8F%E6%9C%8B%E5%8F%8B%E4%BB%AC%E5%88%86%E7%B3%96%E6%9E%9CI/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139380754" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/distribute-candies-among-children-i/solutions/2797856/letmefly-2928gei-xiao-peng-you-men-fen-t-u01r/" target="_blank">LeetCode题解</a>|
878878
|2931.购买物品的最大开销|困难|<a href="https://leetcode.cn/problems/maximum-spending-after-buying-items/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/12/12/LeetCode%202931.%E8%B4%AD%E4%B9%B0%E7%89%A9%E5%93%81%E7%9A%84%E6%9C%80%E5%A4%A7%E5%BC%80%E9%94%80/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/144428274" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-spending-after-buying-items/solutions/3018377/letmefly-2931gou-mai-wu-pin-de-zui-da-ka-gjga/" target="_blank">LeetCode题解</a>|
879879
|2938.区分黑球与白球|中等|<a href="https://leetcode.cn/problems/separate-black-and-white-balls/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/06/06/LeetCode%202938.%E5%8C%BA%E5%88%86%E9%BB%91%E7%90%83%E4%B8%8E%E7%99%BD%E7%90%83/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139511813" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/separate-black-and-white-balls/solutions/2803386/letmefly-2938qu-fen-hei-qiu-yu-bai-qiu-h-6w6y/" target="_blank">LeetCode题解</a>|
880+
|2942.查找包含给定字符的单词|简单|<a href="https://leetcode.cn/problems/find-words-containing-character/" target="_blank">题目地址</a>|<a href="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/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/148197875" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-words-containing-character/solutions/3685237/letmefly-2942cha-zhao-bao-han-gei-ding-z-tv19/" target="_blank">LeetCode题解</a>|
880881
|2944.购买水果需要的最少金币数|中等|<a href="https://leetcode.cn/problems/minimum-number-of-coins-for-fruits/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/01/24/LeetCode%202944.%E8%B4%AD%E4%B9%B0%E6%B0%B4%E6%9E%9C%E9%9C%80%E8%A6%81%E7%9A%84%E6%9C%80%E5%B0%91%E9%87%91%E5%B8%81%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145340613" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-coins-for-fruits/solutions/3056265/letmefly-2944gou-mai-shui-guo-xu-yao-de-la0i8/" target="_blank">LeetCode题解</a>|
881882
|2951.找出峰值|简单|<a href="https://leetcode.cn/problems/find-the-peaks/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/05/28/LeetCode%202951.%E6%89%BE%E5%87%BA%E5%B3%B0%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/139279605" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-the-peaks/solutions/2793708/letmefly-2951zhao-chu-feng-zhi-mo-ni-bia-5jn1/" target="_blank">LeetCode题解</a>|
882883
|2952.需要添加的硬币的最小数量|中等|<a href="https://leetcode.cn/problems/minimum-number-of-coins-to-be-added/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/30/LeetCode%202952.%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E7%A1%AC%E5%B8%81%E7%9A%84%E6%9C%80%E5%B0%8F%E6%95%B0%E9%87%8F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/137185903" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/minimum-number-of-coins-to-be-added/solutions/2716158/letmefly-2952xu-yao-tian-jia-de-ying-bi-bxfa4/" target="_blank">LeetCode题解</a>|
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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>&nbsp;开始的字符串数组&nbsp;<code>words</code>&nbsp;和一个字符&nbsp;<code>x</code>&nbsp;。</p>
13+
14+
<p>请你返回一个 <strong>下标数组</strong>&nbsp;,表示下标在数组中对应的单词包含字符 <code>x</code>&nbsp;。</p>
15+
16+
<p><b>注意</b>&nbsp;,返回的数组可以是&nbsp;<strong>任意</strong>&nbsp;顺序。</p>
17+
18+
<p>&nbsp;</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>&nbsp;</p>
45+
46+
<p><strong>提示:</strong></p>
47+
48+
<ul>
49+
<li><code>1 &lt;= words.length &lt;= 50</code></li>
50+
<li><code>1 &lt;= words[i].length &lt;= 50</code></li>
51+
<li><code>x</code>&nbsp;是一个小写英文字母。</li>
52+
<li><code>words[i]</code>&nbsp;只包含小写英文字母。</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)

WhatIsMore.md

Lines changed: 0 additions & 14 deletions
This file was deleted.

newSolution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Author: LetMeFly
33
Date: 2022-07-03 11:21:14
44
LastEditors: LetMeFly.xyz
5-
LastEditTime: 2025-05-24 15:45:49
5+
LastEditTime: 2025-05-24 21:28:48
66
Command: python newSolution.py 102. 二叉树的层序遍历
77
What's more: 当前仅支持数字开头的题目
88
What's more: 代码结构写的很混乱 - 想单文件实现所有操作
@@ -283,7 +283,7 @@ def getPrOrIssueMaxNum(prOrIssue: str) -> int: # (#811)
283283
commitMsg += commitMsgFromfile
284284
subprocess.run(['git', 'commit', '-s', '-m', commitMsg]) # os.system('git commit -s -m "{msg}"')的话没法评论多行
285285
os.system(f'git push --set-upstream origin {num}')
286-
cmd = f'gh pr create -H {num} -t "添加问题“{num}.{title}”的代码和题解" -B nought-array -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{lastSHA}/newSolution.py) using GH on {getPlatform()} | close: #{issueNum}" -l "题解" -a "@me"' # -H branch可能是 新版/旧版/Mac 所需的属性,并没有默认使用当前分支诶
286+
cmd = f'gh pr create -H {num} -t "添加问题“{num}.{title}”的代码和题解" -b "By [newSolution.py](https://github.com/LetMeFly666/LeetCode/blob/{lastSHA}/newSolution.py) using GH on {getPlatform()} | close: #{issueNum}" -l "题解" -a "@me"' # -H branch可能是 新版/旧版/Mac 所需的属性,并没有默认使用当前分支诶
287287
print(cmd)
288288
prResult = os.popen(cmd).read()
289289
print(prResult)

testTime.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)