我想檢查它存在或不存在的數據值的組合。下面的代碼工作正常,但它看起來效率低下。我正在尋找這個問題的一些很好的解決方案。從行組合
public static void main(String args[]) {
Integer data[] = {
1, 2, 5, 1, 9, 3, 5, 3, 2
};
Integer combination[] = {
1, 3 ,2
};
System.out.println("Result: " + combinations(data, combination));
}
public static boolean combinations(Integer dataset[], Integer combination[]) {
boolean results[] = new boolean[combination.length];
Integer count = 0;
for (Integer comb : combination) {
for (Integer data : dataset) {
if (data.equals(comb)) {
results[count++] = true;
break;
}
}
}
for (Boolean result : results) {
if (!result) {
return false;
}
}
return true;
}
強文本
如果你的代碼的工作,你正在尋找更好,更有效的解決方案,考慮http://codereview.stackexchange.com/ –