问题背景
给你一个下标从
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
≤
w
o
r
d
s
[
i
]
.
l
e
n
g
t
h
≤
50
1 \\le words[i].length \\le 50
-
x
x
-
w
o
r
d
s
[
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;
}
}
评论前必须登录!
注册