My concise and easy to understand Java solution without using long


  • 3
    C
    public class Solution {
        public boolean isPerfectSquare(int num) {
            if (num < 0) return false;
            if (num == 0) return true;
            int start = 1, end = num, mid = 0;
            while (start <= end) {
                mid = (end - start) / 2 + start;
                if (mid * mid > num || mid * mid <= 0) end = mid - 1;
                else if (mid * mid < num ) start = mid + 1;
                else return true;
            }
            return start * start == num;
        }
    }

  • 0
    O

    When input is 2147395600, it will not get the right answer.


Log in to reply
 

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