Find the missing and duplicate elements in an array in linear time and constant space

后端 未结 8 1331
青春惊慌失措
青春惊慌失措 2020-11-27 14:10

You’re given an array of N 64 bit integers. N may be very large. You know that every integer 1..N appears once in the array, except there is one integer mis

8条回答
  •  北海茫月
    2020-11-27 14:31

    Here is a Java implementation based on @Aryabhatta 's idea:
    Input:[3 1 2 5 3]
    Output:[3, 4]

    public ArrayList repeatedNumber(final List A) {
        ArrayList ret = new ArrayList<>();
        int xor = 0, x = 0, y = 0;
        for(int i=0; i

提交回复
热议问题