Using Collections.binarySearch


  • 0

    Just for fun.

    public int firstBadVersion(int n) {
        return Collections.binarySearch(new ArrayList() {
            public int size() {
                return n + 1;
            }
            public Integer get(int i) {
                return (isBadVersion(i-1) ? 1 : 0) + (isBadVersion(i) ? 1 : 0);
            }
        }, 1);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.