我刚接受采访,面试官问我以下问题:
从String数组(英文单词)中,编写一个返回包含字符的所有单词的函数。
我最初提出的建议如下:
public static List<String> find(String[] array, char c) {
if(null == array || array.length == 0)
return null;
List<String> res = new ArrayList<String>();
for(String s : array) {
if(s.indexOf(c) > -1) {
res.add(s);
}
}
return res;
}
然后,采访者让我优化我的解决方案,可以使用什么样的数据结构。 (她说我的解决方案是蛮力)
我仍然没有看到如何优化这一点。有任何想法吗?
答案 0 :(得分:2)
你不能进一步优化这个功能,但我想面试官想听到这样的话:
如果我们需要在同一个数组上多次执行此操作,我们可以使用以下类(以伪代码编写):
class FastStringArrayContainsChar {
private Map<Char, Set<String>> index = new HashMap<Char, Set<String>>();
public FastStringArrayContainsChar(String[] input) {
for (String s: input) {
for (int i=0; i<s.length; s++) {
char c = s.charAt(i);
if (index.contains(c))
index.get(c).put(s);
else
index.put(c, new HashSet<String>(){{this.put(s);}});
}
}
}
public List<String> containsChar(char c) {
return new ArrayList<String>(index.get(c));
}
}
答案 1 :(得分:0)
要检查ith
字符串是否包含字符c
,请逐字检查并在找到后返回。
boolean check(String s, char c)
{ for(int i = 0, n = s.length(); i < n; i++)
if(s[i] == c)
return true;
return false;
}
这不会改变最坏情况的时间,但可能会在实践中快速运行。除此之外,我不知道如何进一步改进代码(我假设没有给出关于数组中单词的其他信息)。
答案 2 :(得分:0)
也许输入数组包含重复项。它虽然使用了更多的内存
public static List<String> find(String[] array, char c) {
if(array == null || array.length == 0) {
return null;
}
Map<String, Boolean> map = new HashMap<String, Boolean>();
List<String> res = new ArrayList<String>();
for(String s : array) {
if (map.containsKey(s)) {
if (map.get(s)) {
res.add(s);
}
} else {
if(s.indexOf(c) > -1) {
map.put(s, true);
res.add(s);
} else {
map.put(s, false);
}
}
}
return res;
}