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
Returns: True
Example 2:
Input: 14
Returns: False
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
不知道搜到最后怎么用,其实就是得到一个区间范围,用左边或者右边
[一句话思路]:
二分法得到一个区间,最后判断两端就行了
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
二分法得到一个区间,最后判断两端就行了
[复杂度]:Time complexity: O(lgn) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
start end最好是long型,避免溢出
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public boolean isPerfectSquare(int num) {
//bs
long start = 1, end = num;
while (start + 1 < end) {
long mid = start + (end - start) / 2;
if (mid * mid < num) {
start = mid;
}
if (mid * mid >= num) {
end = mid;
}
}
//return s or e
if (end * end == (long)num || start * start == (long)num) return true;
return false;
}
}
367. Valid Perfect Square判断是不是完全平方数的更多相关文章
- [LeetCode]367. Valid Perfect Square判断完全平方数
方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...
- 367 Valid Perfect Square 有效的完全平方数
给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False.注意:不要使用任何内置的库函数,如 sqrt.示例 1:输入: 16输出: True示例 ...
- 【easy】367. Valid Perfect Square 判断是不是平方数
class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...
- 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验证完全平方数
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 ...
随机推荐
- python学习之数据结构
python的数据很丰富,所以对于数据分析来讲, python是一种最合适的选择 下面讲述一下常见的数据结构,包括栈,队列,元组,字典,集合等,以及对这些数据结构进行操作 #堆栈,后进先出 a=[10 ...
- 【spring源码学习】springMVC之映射,拦截器解析,请求数据注入解析,DispatcherServlet执行过程
[一]springMVC之url和bean映射原理和源码解析 映射基本过程 (1)springMVC配置映射,需要在xml配置文件中配置<mvc:annotation-driven > ...
- 【3】SpringMVC的Controller
1SpringMvc的Controller是线程安全的吗? (1)由于是单例,tomcat的多线程环境访问,属性必须是不可变的,如果可变,会产生脏数据,线程不安全 2Spring的事务管理 (1)ao ...
- PHP Tools for VS2017 key/破解 [搬运]
看看结果 搬运地址 : (自己敲吧...) 这里面破解的只有一年 时间可以在文中提供的 ------------------------------------------------------- ...
- php无wsdl webservice服务用法
服务端: <?php class test { function add($a,$b) { return $a+$b; } } function getUserInfo($name) { ret ...
- MVC FileDownLoad
public ActionResult MatDownload() { string ShopId = Session["ShopId"].ToString(); var self ...
- Mac电脑Tomcat下载及安装(详细)
下载Tomcat 1.打开Apache Tomcat官网,选择你需要的版本进行下载: 地址http://tomcat.apache.org/download-70.cgi 2.解压apache-t ...
- java排序。。简单的冒泡排序
总结:一种简单的交换顺序,从数左边开始扫描待排序的元素,在扫描过程中依次对相邻元素进行比较,将较大值后移,每经过一轮排序后,值最大的元素将移到末尾, 此时记下该元素的位置,下一轮排序只需比较到此位置即 ...
- 权益保护-知识产权:知识产权(IP)百科
ylbtech-权益保护-知识产权:知识产权(IP)百科 知识产权,也称其为“知识所属权”,指“权利人对其智力劳动所创作的成果和经营活动中的标记.信誉所依法享有的专有权利”,一般只在有限时间内有效.各 ...
- PHP大小写:函数名和类名不区分,变量名区分
PHP对大小写敏感问题的处理比较乱,写代码时可能偶尔出问题,所以这里总结一下. 但我不是鼓励大家去用这些规则.推荐大家始终坚持“大小写敏感”,遵循统一的代码规范. 1. 变量名区分大小写 <?p ...