简体   繁体   English

Java哈希集与Arrays.sort

[英]Java hashset vs Arrays.sort

I am trying to solve the below 'codility' exercise: 我正在尝试解决以下“ codility”练习:

A zero-indexed array A consisting of N different integers is given. 给出了由N个不同整数组成的零索引数组A。 The array contains integers in the range [1..(N + 1)], which means that exactly one element is missing. 该数组包含[1 ..(N + 1)]范围内的整数,这意味着恰好缺少一个元素。

Your goal is to find that missing element. 您的目标是找到缺少的元素。

Write a function: 编写一个函数:

class Solution { public int solution(int[] A); }

that, given a zero-indexed array A, returns the value of the missing element. 给定零索引数组A,则返回缺少元素的值。

For example, given array A such that: 例如,给定数组A使得:

  A[0] = 2
  A[1] = 3
  A[2] = 1
  A[3] = 5

the function should return 4, as it is the missing element. 该函数应返回4,因为它是缺少的元素。

Assume that: 假使,假设:

    N is an integer within the range [0..100,000];
    the elements of A are all distinct;
    each element of array A is an integer within the range [1..(N + 1)].

Complexity: 复杂:

    expected worst-case time complexity is O(N);
    expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified. 输入数组的元素可以修改。

I came up with two solutions: 我想出了两种解决方案:

1) Gives 100%/100% 1)给予100%/ 100%

class Solution {

    public int solution(int[] A) {
        int previous = 0;
        if (A.length != 0) {
            Arrays.sort(A);
            for (int i : A) {
                if (++previous != i) {
                    return previous;
                }
            }
        }
        return ++previous;
    }
}

2) Gives an error WRONG ANSWER, got 65536 expected 100001 2)给出错误WRONG ANSWER,预期得到65536 100001

class SolutionHS {

    public int solution(int[] A) {
        int previous = 0;
        HashSet<Integer> hs = new HashSet<>();
        if (A.length != 0) {
            for (int a : A) {
                hs.add(a);
            }

            for (Integer i : hs) {
                if (++previous != i) {
                    return previous;
                }
            }
        }
        return ++previous;
    }
}

My question is: Shouldn't both approaches (using hashset and Arrays.sort) work the same way? 我的问题是:两种方法(使用hashset和Arrays.sort)是否应该以相同的方式工作? If not can you tell me what the difference is? 如果不能,您能告诉我有什么区别吗?

HashSet is not sorted, so when you iterate over the elements of the Set, you don't get them in an ascending order, as your code expects. HashSet不会排序,因此,当您遍历Set的元素时,不会像代码期望的那样将它们按升序排列。 If you used a TreeSet instead of HashSet , your code would work. 如果使用TreeSet而不是HashSet ,则代码将起作用。

The HashSet solution will give the correct answer if you change the second loop to : 如果将第二个循环更改为: HashSet解决方案将给出正确的答案:

for (int i = 0; i <= A.length; i++) {
    if (!hs.contains(i)) {
        return i;
    }
}

This loop explicitly checks whether each integer in the relevant range appears in the HashSet and returns the first (and only one) which doesn't. 此循环显式检查相关范围内的每个整数是否出现在HashSet并返回没有出现的第一个(也是唯一一个)。

Anyway, both your implementations don't meet the O(n) running time and O(1) space requirements. 无论如何,您的两个实现都不满足O(n)运行时间和O(1)空间要求。

In order you meet the required running time and space, you should calculate the sum of the elements of the array and subtract that sum from (A.length+1)*A.length/2 . 为了满足所需的运行时间和空间,应该计算数组元素的总和,然后从(A.length+1)*A.length/2减去该总和。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM