367. Valid Perfect Square
原题:
读题:
求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题
求解方法1:812ms
class Solution {
public:
bool isPerfectSquare(int num)
{
if(num < 0) return false;
int i = 0;
//这里要加1,不然num = 1时会出错
for(;i< num/2 + 1;i++)
{
if(i*i == num)
{
return true;
}
}
return false;
}
};
论坛高效率求解法:
class Solution {
public:
bool isPerfectSquare(int num) {
long left = 1, right = num;
while(left <= right){
long mid = left + (right - left) / 2;
long t = mid * mid;
if(t > num){
right = mid - 1;
}else if(t < num){
left = mid + 1;
}else
return true;
}
return false;
}
};
367. Valid Perfect Square的更多相关文章
- [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
题目描述: 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 ...
- 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 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...
- Python 解LeetCode:367. Valid Perfect Square
题目描述:给出一个正整数,不使用内置函数,如sqrt(),判断这个数是不是一个数的平方. 思路:直接使用二分法,貌似没啥好说的.代码如下: class Solution(object): def is ...
- 【easy】367. Valid Perfect Square 判断是不是平方数
class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...
随机推荐
- java基础阶段关于密码或账号字符数字的判断总结
将字符串转成字符数组 首字母判断 思路:应该如何获取首字母 arr[0]为数组第一个元素即是首字母 数字判断true为数字false为非数字 "0123456789".contai ...
- lightgbm的sklearn接口和原生接口参数详细说明及调参指点
class lightgbm.LGBMClassifier(boosting_type='gbdt', num_leaves=31, max_depth=-1, learning_rate=0.1, ...
- front-end architecture
这些东西都需要管理,并且提供一种比较好的方案去维护.在JavaScript被模块化之后,也可以通过单元测试来控制它们的质量,并且把这个过程自动化,每次版本有变更之前,保证它们最基本的正确性.最终,需要 ...
- linux中 shell编程 判断服务是否运行
判断nginx是否运行中: if ps -ef|grep "nginx"|egrep -v grep >/dev/null then echo ok! else echo n ...
- spark dataFrame withColumn
说明:withColumn用于在原有DF新增一列 1. 初始化sqlContext val sqlContext = new org.apache.spark.sql.SQLContext(sc) 2 ...
- WebView加载失败或网络异常时,替换WebView的错误界面;
WebView在加载失败时会显示一个失败原因的界面,各个手机显示的界面还都不一样,部分手机还会把Url显示出来:我们要做的就是统一加载失败的界面: 大概思路:在WebView这个控件上面再覆盖一个Vi ...
- web分页打印
添加css: page-break-before:always 实现分页 window.print()//实现打印
- centos下部署启动elasticsearch错误集合与解决方案
问题一: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144] 解决步 ...
- 3-scala高级
1.模式匹配 //①简单表示: sign = ch match { case '+' => 1 case '-' => -1 case '_' => 0 } //②守卫:(case中 ...
- java BASE64流 输出图片。
亲测3个请求都可用,没有测试性能问题.仅供参考 BASE64Decoder Eclipsse 类可能引用不了解决方案链接:http://blog.csdn.net/JBxiaozi/article/d ...