原题:

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. Mongodb集群搭建之 Replica Set

    Mongodb集群搭建之 Replica Set Replica Set 中文翻译叫做副本集,不过我并不喜欢把英文翻译成中文,总是感觉怪怪的.其实简单来说就是集群当中包含了多份数据,保证主节点挂掉了, ...

  2. nonlocal和global

    获取变量时遵循LEGB原则,修改变量时需要global/nonlocal进行修改 global # global的使用 函数外定义了全局变量: global关键字在函数内会修改全局变量 函数外没定义全 ...

  3. lesson

    需要深入研究:1.pinctrl子系统2.gpio子系统3.dtsi的处理架构4.printk的log级别和log机制5.中断子系统6.console是什么?log来自哪里?7.kernel命令行参数 ...

  4. Android WebView无法播放视频或直播,关闭界面后任在播放的问题;

    1.设置webview属性: webView.setWebChromeClient(new MyWebChromeClient());         webSettings = webView.ge ...

  5. Ddr2,ddr3,ddr4内存条的读写速率

    理论极限值是可以计算的:1333MHz * 64bit(单通道,双通道则128bit) / 8(位到字节单位转换) = 10.664GB/s.这只是理论,实际发挥还要看内存控制器,实际上1333单条跑 ...

  6. WPF Image Source 设置相对路径图片

    BitmapImage bt = new BitmapImage(new Uri("Images\\3_u10484.png", UriKind.Relative));this.I ...

  7. 【Selenium-WebDriver问题点】chromeDriver和chrome浏览器版本之间的兼容性问题

    今天早晨因为测试需求,将chrome浏览器更新到最新的65版本,结果之前用的chromeDriver测试计划,都跑不通过了, 所以就在网上找了下,mark下. 最新的chromedriver与chro ...

  8. win10更改hosts文件

    管理员身份运行notepad,打开hosts文件即可.

  9. myeclipse内存调整

    内存调整: myeclipse.ini里配置后 1.设置Default VM Arguments 在myEclipse中,打开Windows-> Preferences->Java-> ...

  10. 代码:CSS仿制 苹果按钮图标

    首先,先复习一下:CSS的线性渐变.径向渐变 .linear{ background-image:-webkit-linear-gradient(90deg,#f8f8f8 20%,#dae9fa 9 ...