给定一个正整数 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. 消息队列RabbitMQ使用教程收集

    学习应该要系统,最好的方式是看书. RabbitMQ最权威的教程应该参考官方文档. 下面是收集的一些教程: 官方: https://www.rabbitmq.com/getstarted.html h ...

  2. symfony 数据库中文乱码

    这个问题 是由于编辑器没有设置utf8格式造成的,当然config里也要设置utf8 解决方法:编辑器设置utf8,重启 doctrine: dbal: driver: pdo_mysql host: ...

  3. Elasticsearch学习系列之介绍安装

    前言 关于ELK搭建的问题,或许你还有些模糊,其实你把我视频里讲的知识点串联起来就明白了.搭建ELK环境,看下面我说的: 首先,先把ES集群搭建起来,建议用CentOS6.5 64位的linux系统, ...

  4. Oracle index unusable和invisible的差别

    摘录自11g的官方文档:      UNUSABLE Specify UNUSABLE to mark the index or index partition(s) or index subpart ...

  5. eclipse中j2ee(struts2)部署及相关问题释疑

    1.eclipse中进行web项目开发时.部署的时候和利用myeclipse部署时有非常大不同,由于在myeclipse的工具栏中有一个部署button.而且在myeclipse的preference ...

  6. VMWare无法共享文件夹(Win7宿主机\Ubuntu14.04客户机)

    在安装VMWare tools的时候,需要执行 vmware-install.pl.在安装过程中,需要编译vmhgfs module,如果编译失败就很可能导致共享文件夹无法正常挂载. 最近,我在虚拟机 ...

  7. 读书笔记:Information Architecture for the World Wide Web, 3rd Edition 北极熊 简介

    书籍介绍 Information Architecture for the World Wide Web, 3rd Edition<web信息架构:设计大型网站(第三版)> Designi ...

  8. JSON入门指南

    JSON 即 JavaScript Object Natation,它是一种轻量级的数据交换格式,很适合于server与 JavaScript 的交互.本文将高速解说 JSON 格式.并通过代码演示样 ...

  9. NSArray和NSMutableArray的常用方法 (转)

    NSArray和NSMutableArray的常用方法 (转) (2013-09-06 15:13:46) 标签: it 分类: ios编程 初始化方法:   1.init返回一个空数组    2.i ...

  10. 内容原发网站seo不重视2个标签,导致seo效果不如转发网站

    采集数据,挖掘观点,小心求证,得出结论 时间经过 今日凌晨,爬虫热点采集,其中第一财经是目标站之一,采集到了 http://www.yicai.com/news/5391233.html 谷歌去年悄然 ...