当前位置:网站首页>367. 有效的完全平方数-二分法
367. 有效的完全平方数-二分法
2022-06-29 08:45:00 【Mr Gao】
367. 有效的完全平方数
给定一个 正整数 num ,编写一个函数,如果 num 是一个完全平方数,则返回 true ,否则返回 false 。
进阶:不要 使用任何内置的库函数,如 sqrt 。
示例 1:
输入:num = 16
输出:true
示例 2:
输入:num = 14
输出:false
这题我们应该首先去想到二分法,这很重要,可以极大降低时间复杂度,我们可以多多学习这种方法:
bool isPerfectSquare(int num){
int low=0;
int high=num;
long long p=(low+high)/2;
//printf("%d ",p);
while(p*p!=num&&low<=high){
if(p*p>num){
high=p-1;
p=(low+high)/2;
}
else{
low=p+1;
p=(low+high)/2;
}
}
printf("%d ",p);
if(p*p==num){
return true;
}
return false;
}
边栏推荐
- 长安链GO语言智能合约环境搭建及使用
- Summary of 3DMAX jamming, white screen and rendering crash
- LC236. 二叉树的最近公共祖先
- Mongodb persistence
- UE4 动画重定向
- Augfpn: amélioration de l'apprentissage des caractéristiques à plusieurs échelles pour la détection des cibles
- Iso16000-9 testing of volatile organic compounds in building products and furniture
- Ue4 installe le plug - in datasmith dans la version 4.20-23
- Wechat applet project: wechat applet page layout
- 如何将谷歌浏览器设置为默认浏览器
猜你喜欢

How to do unit test well

The former security director of Uber faced fraud allegations and concealed the data leakage event

商业智能BI的未来,如何看待AI+BI这种模式?

Wechat applet sharing page, sharing to the circle of friends

SSD Improvement cfenet

GD32F4xx 以太网芯片(enc28j60)驱动移植

What is hyperfusion? What is the difference with traditional architecture

YOLO Nano:一种高度紧凑的只看一次的卷积神经网络用于目标检测

Matlab tips (21) matrix analysis -- partial least squares regression

UE4 animation redirection
随机推荐
【技术开发】酒精测试仪解决方案开发设计
Pytorch Summary - Automatic gradient
Wechat applet user refuses to authorize geographic location information and calls up the authorization window again
UE4 在viewport视口中显示3D可编辑点
Wechat applet jump to official account image and text content
第十二章 信号(二)- 生产者消费者示例
Mysql配置主从数据库
Print service IP setting scheme
Twinmotion beginner tutorial
SSD Improvement cfenet
[target detection] | indicator a probabilistic challenge for object detection
MH/T 6040航空材料烟密度试验
MATLAB小技巧(21)矩阵分析--偏最小二乘回归
LSM6DSL之SPI驱动
GD32F4xx 以太網芯片(enc28j60)驅動移植
Five heart charity matchmaker team
What is the difference between hyperconverged architecture and traditional architecture?
长安链数据存储介绍及Mysql存储环境搭建
KiCad学习笔记--快捷键
Chapter 12 signals (II) - examples of producers and consumers