给定一个正整数 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. Chrome Plugin Recommendation

    1.AdBlock 拦截广告神器 2.IPBlade 变更IP地址,使你自由 3.JSON-handle 让接口返回的JSON数据更好看 4.Proxy SwitchyOmega 变更浏览器代理 5. ...

  2. [转]MySQL时间与字符串相互转换

    转至:https://www.cnblogs.com/wangyongwen/p/6265126.html 时间.字符串.时间戳之间的互相转换很常用,但是几乎每次使用时候都喜欢去搜索一下用法:本文整理 ...

  3. @RequestMapping与@ModelAttribute 套路

    新接触一个项目,使用了大量注解: 在通过请求路径查看时一直找不到页面的跳转,再查看了文件内所有方法与注解后才找到对应的路径,特此记下: @ModelAttribute("totalfinal ...

  4. 通过ajax把json对象传入后台

    一.前台ajax部分 function icheckDelete(url){ var parms = { list : array //这是个数组 }; $.ajax({ dataType: &quo ...

  5. 开启VS的JavaScript调试

    前提条件,设置我们的IE 去掉勾,即启用调试然后再JS代码片段中输入debugger.注意:如果IE开启了F12调试面板, VS是不会捕获JS代码片段中的debugger断点.

  6. 跨平台技术iOS与安卓

    1.教学资源获取 Flutter的使用教学笔记 2.本地学习笔记

  7. silverlight browse information

    public class Browser { /// <summary> /// During static instantiation, only the Netscape flag i ...

  8. CF580D Kefa and Dishes 状压dp

    When Kefa came to the restaurant and sat at a table, the waiter immediately brought him the menu. Th ...

  9. Qt 学习之路 2(23):自定义事件

    Qt 学习之路 2(23):自定义事件  豆子  2012年10月23日  Qt 学习之路 2  21条评论 尽管 Qt 已经提供了很多事件,但对于更加千变万化的需求来说,有限的事件都是不够的.例如, ...

  10. vs 部署SharePoint项目时, package丢失

    bug描述:vs部署sharepoint项目时报错:重启iis应用池失败,未将对象设置引用到实例. 解决方案:查看项目文件(包括隐藏文件),发现package文件不见了,在回收站内能找到被删除的pac ...