O(lgN) time

a more efficient solution that has O(lgN) time, and O(1) space.