给定一个正整数 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. c#中public,private,protected,internal的区别

    public   可以被外部成员调用  private   只能在被类的成员调用   protected   只能在被类的成员和该类的子类调用   internal   可以在当前项目调用   pub ...

  2. C++11新标准:nullptr关键字

    一.nullptr的意义 1.NULL在C中的定义 #define NULL (void*)0 2.NULL在C++中的定义 #ifndef NULL #ifdef __cplusplus #defi ...

  3. 【Arcgis for android】程序运行出错原因分析及解决(超详细)

    查看项目下是否有libs文件夹,正常情况下其中应该有 如果没有,在项目上右键 ->arcgis tools->convert to arcgis android project 排除了上述 ...

  4. Java连接Hbase异常

    Exception in thread "main" org.apache.hadoop.hbase.client.RetriesExhaustedException: Faile ...

  5. 快速莫比乌斯变换(FMT)

    快速莫比乌斯变换(FMT) 原文出处:虞大的博客.此仅作蒟蒻本人复习用~ 给定两个长度为n的序列 \(a_0, a_1, \cdots, a_{n-1}\)和\(b_0, b_1, \cdots, b ...

  6. Flask 新闻网站

    welcome to visit http://47.94.194.236    最近在搭建django,可能内容有问题,如访问异常,请给我留言! 项目源码托管于gihub 一.项目基本流程: 1.搭 ...

  7. angularJs条件查询:

    首先需要建立一个输入框进行数据绑定: <div class="box-tools pull-right"> <div class="has-feedba ...

  8. 数据结构java学习(三)循环队列

    @TOC 和栈一样,队列也是表,但是使用队列的特点是先进先出. 队列模型 \(\color{black}{队列的基本操作是入队,它是在表的末端插入一个元素,和出队,它是删除在表开头的一个元素}\) g ...

  9. vim简单配置(tab,行号,自动缩进)

    进入到个人目录:cd - 打开vimrc文件:vim .vimrc 在文件中添加以下内容:set shiftwidth=4          #按tab键缩进4个空格set softtabstop=4 ...

  10. SprimgMVC学习笔记(九)—— RESTful支持

    一.什么是restful? Restful就是一个资源定位及资源操作的风格,其核心是面向资源.不是标准也不是协议,只是一种风格.基于这个风格设计的软件可以更简洁,更有层次,更易于实现缓存等机制.RES ...