LeetCode 367.有效的完全平方数(C++)
给定一个正整数 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++)的更多相关文章
- Java实现 LeetCode 367 有效的完全平方数
367. 有效的完全平方数 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False. 说明:不要使用任何内置的库函数,如 sqrt. 示例 1: ...
- Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square)
Leetcode之二分法专题-367. 有效的完全平方数(Valid Perfect Square) 给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 ...
- [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判断完全平方数
方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...
- [LeetCode] 279. Perfect Squares 完全平方数
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- [LeetCode] 0279. Perfect Squares 完全平方数
题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...
- 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_Easy tag:Math
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- [学习笔记]信号基本概念(中断和信号)/名称及常用信号/信号处理/signal函数实践
1基本概念 中断 q 中断是系统对于异步事件的响应 q 中断信号 q 中断源 q 现场信息 q 中断处理程序 q 中断向量表 异步事件的响应:进程执行代码的过程中可以随时被打断,然后去执行 ...
- C# winForm 开机自动启动 并启动后最小化到任务栏 右侧通知栏并交互操作
//设置自动启动 string path = Application.StartupPath; SettingHel.SetAutoRun(path +@"\MostImpressive.D ...
- JAVA基础语法动手动脑实践体会
一.运行EnumTest.java程序. public class EnumTest { public static void main(String[] args) { Size s=Size.SM ...
- java多线程系列:通过对战游戏学习CyclicBarrier
CyclicBarrier是java.util.concurrent包下面的一个工具类,字面意思是可循环使用(Cyclic)的屏障(Barrier),通过它可以实现让一组线程到达一个屏障(也可以叫同步 ...
- SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)
Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...
- UWP&WP8.1 基础控件—Button
Button作为最常用的控件,没有特别难的用法,是一个非常简单,可以很快就掌握的控件. Button 基础用法: 同样,在UWP项目中,可以从工具箱中拖拽到面板中进行使用.也可以使用XAML语法进行编 ...
- jupyter notebook 设置默认目录
1.打开 cmd 输入命令 jupyter notebook --generate-config 可以看到生成文件的路径,这个就是生成的配置文件jupyter_notebook_config.py, ...
- [USACO08OPEN]农场周围的道路Roads Around The Farm BZOJ 1621 DFS
Farmer John's cows have taken an interest in exploring the territory around the farm. Initially, all ...
- freemarker常用标签解释
标签一: if else 你可以使用if,elseif和else指令来条件判断是否越过模板的一个部分.这些condition-s必须计算成布尔值,否则错误将会中止模板处理.elseif-s和else- ...
- FlowLayout(流式布局)用法
https://blog.csdn.net/liujun13579/article/details/7771191