C# - binary search using longs


  • 1
        public bool IsPerfectSquare(int num) 
        {
            if (num < 1) return false;
            long low = 0;
            long high = num;
            while (low <= high)
            {
                long mid = (low + high) / 2;
                long square = mid * mid;
                if (square == num) return true;
                if (square < num) low = mid + 1;
                else high = mid - 1;
            }
            return false;
        }
    

Log in to reply
 

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