给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。
注意:不要使用任何内置的库函数,如  sqrt。
示例 1:
输入: 16
输出: True
示例 2:
输入: 14
输出: False

详见:https://leetcode.com/problems/valid-perfect-square/description/

C++:

方法一:

class Solution {
public:
bool isPerfectSquare(int num) {
for(int i=1;i<=num/i;++i)
{
if(i*i==num)
{
return true;
}
}
return false;
}
};

方法二:

class Solution {
public:
bool isPerfectSquare(int num) {
long left=0,right=num;
while(left<=right)
{
long mid=left+(right-left)/2,t=mid*mid;
if(t==num)
{
return true;
}
else if(t<num)
{
left=mid+1;
}
else
{
right=mid-1;
}
}
return false;
}
};

参考:https://www.cnblogs.com/grandyang/p/5619296.html

367 Valid Perfect Square 有效的完全平方数的更多相关文章

  1. 367. Valid Perfect Square判断是不是完全平方数

    [抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  2. 367. Valid Perfect Square

    原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...

  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 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...

  6. Leetcode 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

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  8. [LC] 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判断完全平方数

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

随机推荐

  1. 解决json_encode中文乱码

    在使用json_encode之前把字符用函数urlencode()处理一下,然后再json_encode,输出结果的时候在用函数urldecode()转回来

  2. Analyzing Storage Performance using the Windows Performance Analysis ToolKit (WPT)

    https://blogs.technet.microsoft.com/robertsmith/2012/02/07/analyzing-storage-performance-using-the-w ...

  3. Property 'sqlMapClient' is required

    继承SqlMapClientDaoSupport类的类里面添加如下代码 @Resource(name = "sqlMapClient") private SqlMapClient ...

  4. Oracle 设置用户密码永不过期

    --1.查看用户的proifle,一般是default select username,profile from dba_users; --2.查看概要文件(default)的密码有效期设置 sele ...

  5. delphi异步选择模型编程TCP

    Server端: unit U_FrmServer; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, ...

  6. OJ(Online Judge)

    OJ:它是Online Judge系统的简称,用来在线检测程序源代码的正确性.著名的OJ有RQNOJ.URAL等.国内著名的题库有北京大学题库.浙江大学题库等.国外的题库包括乌拉尔大学.瓦拉杜利德大学 ...

  7. 网络安全-安全散列函数,信息摘要SHA-1,MD5原理

    -----------------------------------------------欢迎查看网络安全连载博客-----------------------------------[网络安全] ...

  8. 水晶报表 Crystal Report 调用存储过程时出错 找不到表 ,解决方法。

    用 CrystalReportViewer1 控件在asp.net的网页上显示报表,假设做报表时调用数据表数据的方式调用是能够成功的.但报表是用存储过程获取数据方式会出现下面错误: 找不到表'RptO ...

  9. UGUI 实现Button长按效果(RepeatButton)

    Tag:加入了一个延迟,在button按下状态一段时间后再開始 repeate using UnityEngine; using UnityEngine.Events; using UnityEngi ...

  10. iOS 保存视频AVAssetWriter

    错误的CMTime导致保存的视频无效,比如: frameTime CMTime 1122 600ths of a second value CMTimeValue 1122timescale CMTi ...