367 Valid Perfect Square 有效的完全平方数
给定一个正整数 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 有效的完全平方数的更多相关文章
- 367. Valid Perfect Square判断是不是完全平方数
[抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- [LeetCode] 367. Valid Perfect Square 检验完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [leetcode]367. Valid Perfect Square验证完全平方数
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- Leetcode 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 【leetcode】367. Valid Perfect Square
题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...
- [LC] 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- [LeetCode]367. Valid Perfect Square判断完全平方数
方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...
随机推荐
- codeforces 691E(矩阵乘法)
E. Xor-sequences time limit per test 3 seconds memory limit per test 256 megabytes input standard in ...
- Git push时报错 ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to......
今天在使用Git回退到之前某个版本的代码时,进行push时出现如下错误: ! [remote rejected] master -> master (pre-receive hook decli ...
- javaweb开发页面数字过长显示科学计数法的问题
1. 检查该字段是否为double类型,如果是,请改成BigDecimal 2.如果是导出excel里面为科学计数法,原页面正常,是因为excel设置的原因,请参考https://jingyan.ba ...
- 关于python内存管理里的引用计数算法和标记-清楚算法的讨论
先记录于此,后续有时间再深究吧: 1.https://www.zhihu.com/question/33529443 2.http://patshaughnessy.net/2013/10/30/ge ...
- Centos7下安装.bin格式
1.检查系统是否已经存在jdk版本,#java –version.javac –version 2.赋予.bin的jdk安装包的执行权限chmod u+x jdk-6u25-linux-x64.bin ...
- vux picker
1.通过实验证明: PopupPicker = TransferDom + Popup + PopupHeader + Picker 2.代码 Picker.vue <!-- Picker 组件 ...
- chosen.jquery.js 搜索框只能从头匹配的解决思路+方法
chosen.jquery.js 搜索框只能从头匹配的解决思路+方法 心急者请直接看下方 总结 ,由于本问题未能找到直接答案,所以只能通过修改源码解决.故将修改源码思路贴出来供大家参考,在遇到其他改源 ...
- centos部署jenkins
1. 实验环境: 操作系统: CentOS Linux release 7.2.1511 (Core) 软件版本: jdk-8u60-linux-x64 apache-tomcat-9.0. ...
- chrome.socket
chrome.socket https://chajian.baidu.com/developer/apps/socket.html#method-create 描述: 使用 chrome.socke ...
- MySQL通过函数获取字符串汉字拼音首字母大写字符串
DELIMITER $$ DROP FUNCTION IF EXISTS `Fun_GetPY`$$ CREATE FUNCTION `HIS`.`Fun_GetPY` (in_string VARC ...