【easy】367. Valid Perfect Square 判断是不是平方数
class Solution {
public:
bool isPerfectSquare(int num) {
/*
//方法一:蜜汁超时……
if (num < 0) return false;
if (num == 1) return true;
for (int i=1;i*i<=num;i++){
if (i*i==num)
return true;
}
return false;
}*/
/*
//方法二:是对的!
if(num < 0) return false;
if(num == 1) return true;
for(int i = 1; i<= num/i;i++){
if(i*i == num) return true;
}
return false;
} */
//方法三:值得学习的【二分查找】
if (num < ) return false;
if (num == ) return true;
int left = ; //注意!
int right = num/; //注意!
long mid;
long val;
while (left <= right){
mid = (left + right)/;
val = mid * mid;
if (val == num) return true;
else if (val > num) right = mid - ;
else left = mid + ;
}
return false;
}
};
【easy】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 ...
- [LeetCode]367. Valid Perfect Square判断完全平方数
方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...
- 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 解题报告(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 e ...
- 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 ...
- [LC] 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
随机推荐
- PWC6345: There is an error in invoking javac. A full JDK (not just JRE) is required
今天在使用jetty运行一个项目的时候报这个错误,仔细看了下,应该是eclipse配置的jdk或者jre出错. 看了下环境变量,发现有些配置没有配置完全. 我个人的解决方法: 在path中,添加%JA ...
- iis+nginx实现负载均衡
简要说明:nginx的简介自行百度. 目的:把用户的请求分到各个服务器减轻压力.nginx把监听的端口的请求平均转到布署了网站的服务器. 一.windows上安装nginx 1.官网下载windows ...
- JVM调优:HotSpot JVM垃圾收集器
HotSpot JVM垃圾收集器 - Snooper - 博客园https://www.cnblogs.com/snooper/p/8718478.html
- k8s简单的来部署一下tomcat,并测试自愈功能
前言: 2018年12月6日 今天终于把k8s运行tomcat打通了,耗了我几天时间一个一个坑踩过来,不容易啊,废话不多说. 先记录一些操作时的错误: <<<<<< ...
- Excel Foundation Install
安装Excel API 函数库 1. 通过下载工具下载函数库 下载 ExcelAPI函数库更新工具 下载 ExcelAPI函数库离线包 ExcelAPI(WPS)函数库离线包 Exc ...
- Djangon的坑
<a href="/del_student/?pk={{ students.pk }}"></a> 在django中当你写入这样的语句是,pk={{ stu ...
- [模板] 数学基础:快速幂/乘/逆元/exGCD/(ex)CRT/(ex)Lucas定理
方便复制 快速乘/幂 时间复杂度 \(O(\log n)\). ll nmod; //快速乘 ll qmul(ll a,ll b){ ll l=a*(b>>hb)%nmod*(1ll< ...
- Nginx-Tomcat搭建负载均衡(转载)
一. 工具 nginx-1.8.0 apache-tomcat-6.0.33 二. 目标 实现高性能负载均衡的Tomcat集群: 三. 步骤 1.首先下载Nginx,要下载稳定版: 2 ...
- [SCOI2006] 数字立方体
题目类型:三维前缀和+同余方程 传送门:>Here< 题意:给出一个立方体,求有多少个子立方体的和为\(k\)的倍数 解题思路 暴力做法:\(O(n^6)\)枚举子立方体 考虑只枚举长和宽 ...
- postgreSQL学习(二):pgsql的一些基础操作
在上一篇文章中我们学习了怎么安装pgsql,安装好了后,我们来学习一下怎么对pgsql进行创建操作以及相关的crud的操作啦 一 创建数据库 $ createdb test 然后你可能会遇到如下的错误 ...