[英]Checking if the 2nd list have any unique elements compared to 1st list
Trying my own brute force solution.尝试我自己的蛮力解决方案。 Now, there is a problem with my code I don't know what the problem is.
现在,我的代码有问题,我不知道问题是什么。 I calculated the problem on my paper perfectly and it works great.
我在我的论文上完美地计算了这个问题,而且效果很好。 It should work as expected.
它应该按预期工作。 But gives unexpected results.
但给出了意想不到的结果。
Problem: Missing Number问题: 缺少号码
Code:代码:
public static List<Integer> getMissingNumber (List<Integer> arr, List<Integer> brr){
Integer[] value = new Integer[brr.size()];
value = brr.toArray(value);
for(int i=0; i<arr.size();i++){
for(int j=0; j<brr.size(); j++){
if(arr.get(i)==brr.get(j)){
value[j]=0;
break;
}
}
}
Arrays.sort(value);
List<Integer> exect_value = new ArrayList<Integer>();
for(int i=0;i<value.length;i++) {
if(value[i]!=-1) {
exect_value.add(value[i]);
}
}
return Arrays.asList(value);
}
Problem I'm Facing here:我在这里面临的问题:
for(int i=0; i<arr.size();i++){
for(int j=0; j<brr.size(); j++){
if(arr.get(i)==brr.get(j)){
value[j]=0;
break;
}
}
}
When I tested for: Taking 2 different input(Function will pass 2 list):当我测试: 采用 2 个不同的输入(函数将通过 2 个列表):
arr[6] : [7,2,5,3,5,3]
brr[8] : [7,2,5,4,6,3,5,3]
output: [4,6]
you have to print elements that is not founded in list_1输出:
[4,6]
你必须打印不在list_1中的元素
It works perfectly for this test case它非常适合这个测试用例
But但
When I try for: Input:当我尝试:输入:
arr[10] =[11 4 11 7 13 4 12 11 10 14]
brr[15] = [11 4 11 7 3 7 10 13 4 8 12 11 10 14 12]
Output gives: [0, 0, 11, 0, 3, 7, 0, 0, 4, 8, 0, 11, 10, 0, 12]
This but I'm expecting -> [0, 0, 0, 0, 3, 7, 0, 0, 0, 8, 0, 0, 10, 0, 12]
extra [11,4,11]
comes on this array.输出给出:
[0, 0, 11, 0, 3, 7, 0, 0, 4, 8, 0, 11, 10, 0, 12]
这但我期待 -> [0, 0, 0, 0, 3, 7, 0, 0, 0, 8, 0, 0, 10, 0, 12]
额外的[11,4,11]
出现在这个数组上。
Why I'm so confused, please help me.为什么我这么困惑,请帮助我。
When I try for: Input:
当我尝试:输入:
arr[10] =[11 4 11 7 13 4 12 11 10 14] brr[15] = [11 4 11 7 3 7 10 13 4 8 12 11 10 14 12]
Output gives:
[0, 0, 11, 0, 3, 7, 0, 0, 4, 8, 0, 11, 10, 0, 12]
This but I'm expecting ->[0, 0, 0, 0, 3, 7, 0, 0, 0, 8, 0, 0, 10, 0, 12]
extra[11,4,11]
comes on this array.输出给出:
[0, 0, 11, 0, 3, 7, 0, 0, 4, 8, 0, 11, 10, 0, 12]
这但我期待 ->[0, 0, 0, 0, 3, 7, 0, 0, 0, 8, 0, 0, 10, 0, 12]
额外的[11,4,11]
出现在这个数组上。
That is because here:那是因为这里:
for(int i=0; i<arr.size();i++){ for(int j=0; j<brr.size(); j++){ if(arr.get(i)==brr.get(j)){ value[j]=0; break; } } }
you are comparing arr.get(i)
with brr.get(j)
instead of value[j]
.您正在比较
arr.get(i)
与brr.get(j)
而不是value[j]
。
PS. PS。 I seems that the proper solution is to sort and "anti-merge" two lists:
我似乎正确的解决方案是对两个列表进行排序和“反合并”:
public static List<Integer> missingNumbers(List<Integer> arr, List<Integer> brr) {
// Write your code here
Collections.sort(arr);
Collections.sort(brr);
Set<Integer> result = new LinkedHashSet<>();
int l = 0, r = 0;
while (l < arr.size() && r < brr.size()) {
if (arr.get(l).equals(brr.get(r))) {
l++;
r++;
} else {
result.add(brr.get(r++));
}
}
for (; r < brr.size(); r++) {
result.add(brr.get(r));
}
return new ArrayList<>(result);
}
Maybe you can do some like that:也许你可以这样做:
Change "==" for "equals()"将“==”更改为“equals()”
Set in brr array zero when some value has already been stored (because your problem was that you were comparing values of arr that had already been validated).当已经存储了一些值时,将 brr 数组设置为零(因为您的问题是您正在比较已经验证的 arr 值)。 For example, you compared arr in position 2 against brr position 0 (Both values 11), then put 0 back where one already existed.
例如,您将位置 2 的 arr 与位置 0 的 brr 进行比较(两个值都是 11),然后将 0 放回已经存在的位置。
Integer[] value = new Integer[brr.size()]; value = brr.toArray(value); for(int i=0; i<arr.size();i++){ for(int j=0; j<brr.size(); j++){ if(arr.get(i).equals(brr.get(j))){ brr.set(j, 0); value[j]=0; break; } } } Arrays.sort(value); List<Integer> exect_value = new ArrayList<Integer>(); for(int i=0;i<value.length;i++) { if(value[i]!=0) { exect_value.add(value[i]); } } return exect_value;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.