Java 行的组合
我想检查数据中的值组合是否存在。下面的代码运行良好,但看起来效率低下。我正在寻找解决这个问题的好办法Java 行的组合,java,algorithm,Java,Algorithm,我想检查数据中的值组合是否存在。下面的代码运行良好,但看起来效率低下。我正在寻找解决这个问题的好办法 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
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;
}
强文本对未登录的数据集进行排序。快速排序还是合并排序?然后在数据集上对组合数组的每个成员运行二进制搜索。二进制搜索复杂度logn。当您为组合的每个成员执行此操作时,阵列复杂性将为nlogn nlogn+nlogn=2nlogn,这是Onlogn。这样你的表现就会提高 代码
对复杂度为nlogn的数据集进行排序。快速排序还是合并排序?然后在数据集上对组合数组的每个成员运行二进制搜索。二进制搜索复杂度logn。当您为组合的每个成员执行此操作时,阵列复杂性将为nlogn nlogn+nlogn=2nlogn,这是Onlogn。这样你的表现就会提高 代码
由于您的数据由实现Compariable的整数组成,因此我建议您利用这些整数构造一个树集,并将所有数据添加到树集中。然后,可以运行contains来查找树集中是否存在元素。 TreeSet保证每次添加、删除和包含日志的成本。 添加所有元素n将使您失去登录。
查找是否存在“m”元素需要花费mlogn。由于您的数据由实现可比性的整数组成,我建议您利用这一点,构建一个树集,并将所有数据添加到其中。然后,可以运行contains来查找树集中是否存在元素。 TreeSet保证每次添加、删除和包含日志的成本。 添加所有元素n将使您失去登录。
查找是否存在“m”元素需要花费mlogn。您可以从一个数组更改为一个数组列表,并使用containsAll方法。不确定它是否足够有效,但会使代码更短
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[]) {
List<Integer> data = Arrays.asList(dataset);
List<Integer> comb = Arrays.asList(combination);
return data.containsAll(comb);
}
您可以从array更改为ArrayList,并使用containsAll方法。不确定它是否足够有效,但会使代码更短
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[]) {
List<Integer> data = Arrays.asList(dataset);
List<Integer> comb = Arrays.asList(combination);
return data.containsAll(comb);
}
看起来您只是想检查组合是否是数据集的子集……它在数据集中出现的顺序并不重要。对吗 您的数据集有多大?数据集是在需要时创建的还是始终保持的 若数据集很大,并且始终保持不变,那个么若您能够对其进行排序,那个么搜索就会更容易
for (Integer comb : combination) {
if (Arrays.binarySearch(dataset, comb) < 0)
return false; //If any element is not found, return false
}
}
return true;
看起来您只是想检查组合是否是数据集的子集……它在数据集中出现的顺序并不重要。对吗 您的数据集有多大?数据集是在需要时创建的还是始终保持的 若数据集很大,并且始终保持不变,那个么若您能够对其进行排序,那个么搜索就会更容易
for (Integer comb : combination) {
if (Arrays.binarySearch(dataset, comb) < 0)
return false; //If any element is not found, return false
}
}
return true;
TeeSeET.CaseSuth.Eclipse看起来都很有效率。如果你的代码工作,你正在寻找更好和更有效的解决方案,考虑一下你的问题,如果你的代码工作,你正在寻找更好和更有效的解决方案,考虑把你的问题放在一个校正上…返回前两个代码块的真实值应该在外进行循环校正…返回前两个代码块中的真值应该在循环外。
boolean isMatch = false;
for (Integer comb : combination) {
//We will be here in two condition -
// 1. first iteration or previous element was found in dataset.
// 2. Just set to false to check next element. Reset the flag
boolean isMatch = false;
for (Integer data : dataset) {
if (data.equals(comb)) {
//match found for the current combination element
isMatch = true;
break;
}
}
//This mean one of the combination element is not there. Break out.
if (!isMatch) break;
}
return isMatch;