原题:

367. Valid Perfect Square

读题:

求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题

求解方法1:812ms

class Solution {
public:
bool isPerfectSquare(int num)
{
if(num < 0) return false;
int i = 0;
//这里要加1,不然num = 1时会出错
for(;i< num/2 + 1;i++)
{
if(i*i == num)
{
return true;
}
}
return false;
}
};

论坛高效率求解法:

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

  

367. Valid Perfect Square的更多相关文章

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

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

  2. Leetcode 367. Valid Perfect Square

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

  3. 【leetcode】367. Valid Perfect Square

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

  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. 367. Valid Perfect Square判断是不是完全平方数

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

  6. [LC] 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. [UE4]C++代码实现播放粒子特效

    转自:http://aigo.iteye.com/blog/2273345 函数参数说明(在GameplayStatics.h中) 将一个粒子放到指定位置上播放: 另一种重载形式: 将一个粒子atta ...

  2. IDEA配置打可运行jar包

    IDEA打包可以运行的jar包大体有两种方式:一种是比较方便的配置maven:一种是直接配置IDEA采用Build Artifacts打包. 配置maven打包,在pom.xml里面配置build插件 ...

  3. 用python进行桌面程序开发

    Python是一种面向对象.直译式计算机程序设计语言,也是一种功能强大而完善的通用型语言,已经具有十多年的发展历史,成熟且稳定.这种语言具有非常简捷而清晰的语法特点,适合完成各种高层任务,几乎可以在所 ...

  4. python学习笔记之一

    1.可以自己运行,也可以被import后调用 if __name__ == '__main__' main() 2.可变参数,关键字参数,命名关键字参数 可变参数和关键字参数 def f1(a, b, ...

  5. Linux coredump 的打开和关闭

    (转载自 http://blog.sina.com.cn/s/blog_6b3765230100lazj.html) ulimit -c 输出如果为0,则说明coredump没有打开 ulimit - ...

  6. MapReduce的工作机制

    <Hadoop权威指南>中的MapReduce工作机制和Shuffle: 框架 Hadoop2.x引入了一种新的执行机制MapRedcue 2.这种新的机制建议在Yarn的系统上,目前用于 ...

  7. 数据库类型总结对比(Java,Hibernate,SQLServer,MySQL,Oracle)

  8. PHP:引用Phpword导出数据到word文档

    下载地址https://www.cnwenhui.cn/html/show-597.html(其中有中文使用手册可以下载看看) 1.首先要下载Phpword类库,放在如下图目录下 2.调用方法 pub ...

  9. Spring Boot 整合mybatis-generator

    Maven 引入 mybatis-generator插件 <build> <plugins> <plugin> <groupId>org.springf ...

  10. 使用Inent 携带 Bundle 携带 数组 传递给另外一个activity

    在activity发送数组: public static String [] PhoneAndTime = new String[2]; //new 新的Intent Intent data = ne ...