With a Little Help from My Friends

  • 1

    My friends being the Ruby and Python libraries. Use binary search to find the smallest non-negative integer whose square is at least num. Then test whether its square is num.

    def is_perfect_square(num)
      (0..num).bsearch { |x| x*x >=num } ** 2 == num
    def isPerfectSquare(self, num):
        class C: __getitem__ = lambda _, x: x*x >= num
        return bisect.bisect(C(), False, 0, num) ** 2 == num

Log in to reply

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