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的更多相关文章

  1. 367. Valid Perfect Square

    原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...

  2. 367. Valid Perfect Square判断是不是完全平方数

    [抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  3. [LeetCode] 367. Valid Perfect Square 检验完全平方数

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

  4. Leetcode 367. Valid Perfect Square

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

  5. 【leetcode】367. Valid Perfect Square

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  6. [leetcode]367. Valid Perfect Square验证完全平方数

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

  7. 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...

  8. Python 解LeetCode:367. Valid Perfect Square

    题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方. 思路:直接使用二分法,貌似没啥好说的.代码如下: class Solution(object): def is ...

  9. 【easy】367. Valid Perfect Square 判断是不是平方数

    class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...

随机推荐

  1. JAVA 算法练习(一)

    用java写了几道编程题目,分享给大家 语法和C语言几乎一样,不懂 java 会 c 也可以看明白的. 最大连续数列和 题目说明 对于一个有正有负的整数数组,请找出总和最大的连续数列.给定一个int数 ...

  2. C#——发送邮件

    需要2个引用 using System.Net;using System.Net.Mail; using (MailMessage mailMessige=new MailMessage()) usi ...

  3. eclipse环境配置,字体大小,代码智能提示,JSP页面默认字符集修改

    安装好JDK后,下载Java EE解压版eclipse 1.字体大小 Windows——>Preferences——>General——>Appearance——>Colors ...

  4. uWSGI调整buffer-size

    https://uwsgi-docs.readthedocs.io/en/latest/Options.html#buffer-size buffer-size argument: required_ ...

  5. 写一个读取Excel表格的接口

    # -*- coding: gbk -*-import xlrd class Canshu: def __init__(self,filepath): """ 创建文件对 ...

  6. Samba 文件共享

    介绍:用于Linux系统与Windows系统之间共享文件资源,小文件可以使用lrzsz,大文件可以使用samba 测试环境: [root@localhost home]# cat /etc/redha ...

  7. C++读取数量不定的数据

    #include <iostream> using namespace std; int main(){ ,num=; while(cin >> num){//此表达式从标准输 ...

  8. 关于mysql一边查一边更新

    update test_table set user_id = 112 where id in (select id from ( select id from test_table where nu ...

  9. [Algo] 253. Longest Substring Without Repeating Characters

    Given a string, find the longest substring without any repeating characters and return the length of ...

  10. 吴裕雄--天生自然 pythonTensorFlow图形数据处理:数据集高层操作

    import tempfile import tensorflow as tf # 1. 列举输入文件. # 输入数据生成的训练和测试数据. train_files = tf.train.match_ ...