简体   繁体   English

检查第二个列表与第一个列表相比是否有任何独特的元素

[英]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:也许你可以这样做:

  1. Change "==" for "equals()"将“==”更改为“equals()”

  2. 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.

相关问题 检查第一个 if 条件,然后检查第二个 - Checking 1st if condition and then 2nd 如何基于第一列表中的字段将第二列表与第一列表连接? - How to join 2nd list with first list based on a field in 1st list? 如何用两个参数编写Mybatis XML Mapper(第一个是List <String> ,第二长)? - How to write Mybatis XML Mapper with two parameter (1st is List<String>, 2nd is Long)? Android - 在第一个活动中使用第二个活动的字符串列表 - Android - Use String from 2nd Activities for List on 1st activity 关于第一个init()和第二个init() - regarding 1st init() and 2nd init() 创建2个数组-第一个具有随机整数,第二个具有唯一的随机整数 - Create 2 arrays - 1st has random integers, 2nd has unique random integers 给定一组具有2个值的对象。 相对于第一个值,然后使用第二个值对集合进行排序 - Given a set of objects that have 2 values. Sort the set with respect to the 1st value and then with the 2nd value 使用Enumeration <E>的hasMoreElements()和nextElement()方法打印列表,第1次成功,第2次添加失败后添加了一些元素 - using Enumeration<E> 's hasMoreElements() and nextElement() method to print a list, succeeded 1st time, failed 2nd time after adding some element 从Android中的第一个应用程序开始第二个应用程序的活动 - Start Activity of 2nd app from 1st app in android Java多线程第二线程等待第一 - Java multithreading 2nd thread waiting for 1st
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM