HackerRank缺少数字

时间:2016-04-08 18:20:17

标签: java arraylist iterator

我正在尝试解决此网站中的任务,这里是link

示例输入

  

10

     

203 204 205 206 207 208 203 204 205 206

     

13

     

203 204 204 205 206 207 205 208 203 206 205 206 204

示例输出

  

204 205 206

解释

  

204存在于两个阵列中。它在A中的频率是2,而它的频率是2   B中的频率是3.类似地,205和206在A中出现两次,但是   因此,这三个数字是我们的输出。剩下的   两个列表中的数字频率相同。

4例中有4个测试用例我已经通过了第0,第2和第3测试用例,但是第1个测试用例给出了错误的输出,第4个测试用例给出了timedout错误。请建议我使用好的算法或逻辑来解决这个任务。我帮朋友们。

抱歉我的英文不好请原谅我。

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Integer> al = new ArrayList<Integer>();
        ArrayList<Integer> al1 = new ArrayList<Integer>();
        int size1 = sc.nextInt();
        for( int i = 0 ; i < size1 ; i++){
             al.add(sc.nextInt());
        }


        int size2 = sc.nextInt();
        for( int i = 0 ; i < size2 ; i++){
             al1.add(sc.nextInt());
        }

        Iterator<Integer> ir = al.iterator();
        while(ir.hasNext()){

            int num = ir.next();
            System.out.println("Number"+num);
            if( al1.contains(num) ){
                System.out.println("Before removing number from second arraylist : "+ al1);
                al1.remove(new Integer(num));
                System.out.println("After removing number from second arraylist : "+ al1);
            }
        }
        Collections.sort(al1);
        for(int i : al1)
        System.out.print(i+" ");
    }
}

以下是我的输出:

    Number203
Before removing number from second arraylist : [203, 204, 204, 205, 206, 207, 205, 208, 203, 206, 205, 206, 204]
After removing number from second arraylist : [204, 204, 205, 206, 207, 205, 208, 203, 206, 205, 206, 204]
Number204
Before removing number from second arraylist : [204, 204, 205, 206, 207, 205, 208, 203, 206, 205, 206, 204]
After removing number from second arraylist : [204, 205, 206, 207, 205, 208, 203, 206, 205, 206, 204]
Number205
Before removing number from second arraylist : [204, 205, 206, 207, 205, 208, 203, 206, 205, 206, 204]
After removing number from second arraylist : [204, 206, 207, 205, 208, 203, 206, 205, 206, 204]
Number206
Before removing number from second arraylist : [204, 206, 207, 205, 208, 203, 206, 205, 206, 204]
After removing number from second arraylist : [204, 207, 205, 208, 203, 206, 205, 206, 204]
Number207
Before removing number from second arraylist : [204, 207, 205, 208, 203, 206, 205, 206, 204]
After removing number from second arraylist : [204, 205, 208, 203, 206, 205, 206, 204]
Number208
Before removing number from second arraylist : [204, 205, 208, 203, 206, 205, 206, 204]
After removing number from second arraylist : [204, 205, 203, 206, 205, 206, 204]
Number203
Before removing number from second arraylist : [204, 205, 203, 206, 205, 206, 204]
After removing number from second arraylist : [204, 205, 206, 205, 206, 204]
Number204
Before removing number from second arraylist : [204, 205, 206, 205, 206, 204]
After removing number from second arraylist : [205, 206, 205, 206, 204]
Number205
Before removing number from second arraylist : [205, 206, 205, 206, 204]
After removing number from second arraylist : [206, 205, 206, 204]
Number206
Before removing number from second arraylist : [206, 205, 206, 204]
After removing number from second arraylist : [205, 206, 204]
204 205 206

1 个答案:

答案 0 :(得分:0)

由于您使用ArrayList,解决此问题的简单方法如下:

for (Integer i : al) { // This loops through the array.
    al1.remove(i);
}

// Now al1 have the desired elements, now all you have to do is put them in order.

它不是最有效的,但 是我现在能想到的最简单的代码。

注意:我会使用removeAll,但我不确定它对重复项的作用。