【Leetcode 每日一题】2942. 查找包含给定字符的单词
问题背景
给你一个下标从 0 0 0 开始的字符串数组 w o r d s words words 和一个字符 x x x。
请你返回一个 下标数组 ,表示下标在数组中对应的单词包含字符 x x x。
注意 ,返回的数组可以是 任意 顺序。
数据约束
- 1 ≤ w o r d s . l e n g t h ≤ 50 1 \le words.length \le 50 1≤words.length≤50
- 1 ≤ w o r d s [ i ] . l e n g t h ≤ 50 1 \le words[i].length \le 50 1≤words[i].length≤50
- x x x 是一个小写英文字母。
- w o r d s [ i ] words[i] words[i] 只包含小写英文字母。
解题过程
用库函数就能搞定,不需要想得很复杂。
具体实现
class Solution {public List<Integer> findWordsContaining(String[] words, char x) {List<Integer> res = new ArrayList<>();for (int i = 0; i < words.length; i++) {if (words[i].indexOf(x) >= 0) {res.add(i);}}return res;}
}