给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。

说明:不要使用任何内置的库函数,如  sqrt

示例 1:

输入:16
输出:True

示例 2:

输入:14
输出:False 需要注意mid * mid超过有效范围
#include <iostream>

using namespace std;

bool isPerfectSquare(int num) {
long long left = ,right = num;
long long squ, mid;
while (left <= right) {
mid = (left + right) / ;
//mid = (left + right) >> 1
squ = mid * mid;
if (squ == num)
return true;
else if (squ > num)
right = mid - ;
else
left = mid + ;
} return false;
} int main()
{
cout << boolalpha << isPerfectSquare(); system("PAUSE");
return ;
}

LeetCode 367.有效的完全平方数(C++)的更多相关文章

  1. Java实现 LeetCode 367 有效的完全平方数

    367. 有效的完全平方数 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: ...

  2. Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)

    Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...

  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判断完全平方数

    方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...

  6. [LeetCode] 279. Perfect Squares 完全平方数

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  7. [LeetCode] 0279. Perfect Squares 完全平方数

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  8. Leetcode 367. Valid Perfect Square

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

  9. [LeetCode] 367. Valid Perfect Square_Easy tag:Math

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

随机推荐

  1. Java50道经典习题-程序27 求素数

    题目:求100之内的素数分析:素数即除了1和它本身以外不再有其他因数,最小的素数是2 判断一个数n是否是素数的方法:将n分别与2到(n+1)/2取余,若有一个值为0,则n就不为素数,反之为素数 pub ...

  2. Java50道经典习题-程序49 子串出现的个数

    题目:计算首末不含空格各个子串之间只含一个空格的字符串中子串出现的次数分析:例如输入的字符串为"I come from County DingYuan Province AnHui.&quo ...

  3. windows下部署icescrum

    软件151  卢炜杰 一.安装JDK 1.下载JDK 地址:http://www.oracle.com/technetwork/java/javaee/downloads/index.html 选择相 ...

  4. SQL XML示例

    declare @xmlDoc xml,@id varchar(50); set @xmlDoc='<DocObjContent> <NewCtrl Id="0001&qu ...

  5. python3如何打印进度条

    Python3 中打印进度条(#)信息: 代码: import sys,time for i in range(50): sys.stdout.write("#") sys.std ...

  6. Paste JSON as Code • quicktype 软件的使用

    1.软件图标认知 该软件为json字符串与对象之间相互转户的自动化软件. 下载地址 2.打开软件 配置基本设置 3.生成.h文件 选择生成.h文件 拷贝代码到你管理该对象json文件的.h文件下 4. ...

  7. ECS简介

    https://www.cnblogs.com/yangrouchuan/p/7436533.html Unity下的ECS框架 Entitas简介   最近随着守望先锋制作组在gdc上发布的一个关于 ...

  8. JavaScript之入门篇(二)

    终于学到后面的语法部分了,感觉这门语言基础部分和当初学习VB的时候感觉一样一样的,章节目录让我不禁又想到了VB课本.由于怕学过了,过段时间忘了,于是,大概总结一下. 数据类型部分 ① Typeof操作 ...

  9. MobaXterm替换cmder

    Windows上命令行工具cmder确实很好用,其扩展功能呢,比系统自带强大几倍.后来在使用MobaXterm,官网https://mobaxterm.mobatek.net/免费版本功能足够强大,支 ...

  10. Gym - 101845D 计算几何

    Ignacio is a famous mathematician, some time ago he was married with Dolly, a famous scientific, at ...