第一题
穷尽集合之间的元素组合
public class THS01 {
public static List<List<String>> calculateCombination(List<List<String>> inputList) {
List<String> combineList = new ArrayList<>();
for(int i=0; i<inputList.size(); i++){
combineList = combine(combineList, inputList.get(i));
}
List<List<String>> result = new ArrayList<>();
for(String str : combineList){
if(!str.isEmpty()){
List<String> list = new ArrayList<>();
result.add(list);
for(char ch : str.toCharArray()) {
list.add(String.valueOf(ch));
}
}
}
return result;
}
public static List<String> combine(List<String> strList, List<String> list){
List<String> res = new ArrayList<>();
if(null == strList || strList.size() == 0){
for(String str : list){
res.add(str);
}
return res;
}
for(String str : strList){
for(int j=0; j<list.size(); j++){
res.add(str + list.get(j));
}
}
return res;
}
}
第二题
字典查找的算法
public class DictionarySearcher {
private List<String> wordList = new ArrayList<>();
public DictionarySearcher(String fileName){
File file = new File(fileName);
try {
BufferedReader buff = new BufferedReader(new FileReader(file));
String line = "";
while (null != (line= buff.readLine())) {
wordList.add(line);
}
buff.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public HashMap<String, List<Integer>> search(String document){
HashMap<String, List<Integer>> result = new HashMap<>();
for (int i = 0; i < wordList.size(); i++) {
if (document.contains(wordList.get(i))) {
List<Integer> idxList = new ArrayList<>();
int index = document.indexOf(wordList.get(i));
idxList.add(index);
for (int j = index; j < document.length(); j += wordList.get(i).length()) {
if (document.indexOf(wordList.get(i), j) != -1) {
int pos = document.indexOf(wordList.get(i), j);
if (!idxList.contains(pos)) {
idxList.add(pos);
}
}
}
result.put(wordList.get(i), idxList);
}
}
return result;
}
}