[LC] 367. Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else False.
Note: Do not use any built-in library function such as sqrt.
Example 1:
Input: 16
Output: true
Example 2:
Input: 14
Output: false
class Solution {
public boolean isPerfectSquare(int num) {
if (num < 0) {
return false;
}
int left = 0, right = num;
while (left <= right) {
long mid = left + (right - left) / 2;
if (mid * mid == num) {
return true;
} else if (mid * mid < num) {
left = (int)mid + 1;
} else {
right = (int)mid - 1;
}
}
return false;
}
}
[LC] 367. Valid Perfect Square的更多相关文章
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- 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 ...
- 【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 解题报告(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 ...
随机推荐
- 开始新建AEM站点-周末教程
Getting Started Developing AEM Sites - WKND Tutorial 开始新建AEM站点-周末教程 The goal for this multi-part tut ...
- 用Axure画原型图有感
感觉前端做UE非常有优势啊- 但是在制作的时候,似乎陷入了误区: (1)只求原型图的漂亮,色彩丰富,忽略了其本质作用,是用来整理逻辑,画出逻辑流程的. (2)一开始就追求交互,高保真的原型,忽视了细节 ...
- TX2Ubuntu16.04上安装 kinectV2
本文参考 https://www.ncnynl.com/archives/201706/1780.html 参考 https://blog.csdn.net/qq_33835307/arti ...
- JavaScript学习总结(七)
这一讲我们来学习DOM编程(十分重要),有了DOM编程,我们就可以操作任意的HTML元素了. DOM,文档对象模型 一个html页面被浏览器加载的时候,浏览器就会对整个html页面上的所有标签都会创建 ...
- Creo 2.0 Toolkit 解锁的问题
近期开发Creo Toolkit遇到一个问题,在自己本机开发完成后运行并无问题,但是如果拿去给别人的机子运行会报出 提示“creo ToolKit应用程序在分配到您的地址之前未被解锁”,在与PTC 技 ...
- dp--最长上升子序列LIS
1759:最长上升子序列 总时间限制: 2000ms 内存限制: 65536kB 描述 一个数的序列bi,当b1 < b2 < ... < bS的时候,我们称这个序列是上升的.对 ...
- python 运算符 取余 取商 in not in
#运算符sum = 9//2 #取商print(sum) sum = 9%2 #取余print(sum) #inname1 = '小林'name2 = '林倩'if '林' in name1: pri ...
- 微信小程序2048开发进度(二)
对小程序首先进行游戏架构: 实现小程序的首页展示界面,初始化棋盘格,以及NEW GAME按钮,成绩展现.
- Python—插入排序算法
# 插入排序,时间复杂度O(n²) def insert_sort(arr): """ 插入排序:以朴克牌为例,从小到大排序.摸到的牌current与手里的每张牌进行对比 ...
- P2P平台疯狂爆雷后,你的生活受到影响了吗?
最近这段时间P2P爆雷的新闻和报道一直占据着各大财经和科技媒体的重要位置.而据网贷之家数据显示,截至2018年7月底,P2P网贷行业累计平台数量达到6385家(含停业及问题平台),其中问题平台累计为2 ...