给定一个正整数 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. 多线程学习-基础(一)Thread和Runnable实现多线程

    很久没记录一些技术学习过程了,这周周五的时候偶尔打开“博客园”,忽然让我产生一种重拾记录学习过程的想法,记录下学习研究过程的一点一滴,我相信,慢慢地就进步了!最近想学习一下多线程高并发,但是多线程在实 ...

  2. c#百分比计算

    //此方法得到的百分比后小数太多,不行double percent=Convert.ToDouble(2)/Convert.ToDouble(34); string result=(percent*1 ...

  3. Owin password

    一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorization(授权),而不是Authentication(认证). ...

  4. console的使用

    一.显示信息的命令 console.log("normal"); // 用于输出普通信息 console.info("information"); // 用于输 ...

  5. oracle多用户并发及事务处理

    多用户并发访问 事务:作用于某些数据的一个不可分割的操作   锁:写锁.互斥锁(仅能被一个进程使用)      读锁.共享锁(可被多个进程使用)   更新丢失 脏读 不可重复读 幻影读   隔离级别: ...

  6. P4345 [SHOI2015]超能粒子炮·改 Lucas

    \(\color{#0066ff}{ 题目描述 }\) 曾经发明了脑洞治疗仪与超能粒子炮的发明家 SHTSC 又公开了他的新发明:超能粒子炮・改--一种可以发射威力更加强大的粒子流的神秘装置. 超能粒 ...

  7. zookeeper分布式锁简单实现(JavaApi)

    1.创建会话连接 package com.karat.cn.zookeeperAchieveLock.javaapilock; import org.apache.zookeeper.WatchedE ...

  8. 爬虫框架urllib 之(三) --- urllib模块

    Mac本 需导入ssl import ssl ssl._create_default_https_context = ssl._create_unverified_context  urllib.re ...

  9. Qt 学习之路 2(8):添加动作

    Home / Qt 学习之路 2 / Qt 学习之路 2(8):添加动作       [在WINDOWS10 QTCREATOR MENU添加无效]   Qt 学习之路 2(8):添加动作  豆子   ...

  10. JS模式和原型精解

    首先需要知道的是 模式只是思想.不要用 结构 看模式. ES中函数是对象,因此函数也有属性和方法. 每个函数含有两个属性: length 和 prototype 每个函数含有两个非继承的方法: app ...