码迷,mamicode.com
首页 > 其他好文 > 详细

[LC] 367. Valid Perfect Square

时间:2019-11-17 12:31:52      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:func   input   nbsp   cti   library   perfect   NPU   als   inpu   

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

Example 1:

Input: 16
Output: true

Example 2:

Input: 14
Output: false


class Solution {
    public boolean isPerfectSquare(int num) {
        if (num < 0) {
            return false;
        }
        int left = 0, right = num;
        while (left <= right) {
            long mid = left + (right - left) / 2;
            if (mid * mid == num) {
                return true;
            } else if (mid * mid < num) {
                left = (int)mid + 1;
            } else {
                right = (int)mid - 1;
            }
        }
        return false;
    }
}

 

[LC] 367. Valid Perfect Square

标签:func   input   nbsp   cti   library   perfect   NPU   als   inpu   

原文地址:https://www.cnblogs.com/xuanlu/p/11875871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!