Missing Integer

https://www.lintcode.com/problem/missing-integer/description

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * Definition of BitInteger:
 * public class BitInteger {
 * public static int INTEGER_SIZE = 31;
 * public int fetch(int j) {
 * .... // return 0 or 1, fetch the jth bit of this number
 * }
 * }
 */
public class Solution {
    /**
     * @param array a BitInteger list
     * @return an integer
     */
    public int findMissing(ArrayList array) {
        // Write your code here
        Collections.sort(array, new Comparator() {
            @Override
            public int compare(BitInteger o1, BitInteger o2) {
                int index = 31;
                while (o1.fetch(index) == o2.fetch(index)) {
                    index--;
                }
                return o1.fetch(index) - o2.fetch(index);
            }
        });
        for (int i = 0; i < array.size(); i++) {
            BitInteger bitInteger = array.get(i);
            int fetch1 = bitInteger.fetch(0);
            if (fetch1 != i % 2) {
                return i;
            }
        }
        return array.size();
    }
}

你可能感兴趣的:(Missing Integer)