【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/valid-perfect-square/description/
题目描述
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
题目大意
判断一个数字是不是完全平方数。
解题方法
方法一:完全平方式性质
这个题其实不难,困扰我的是时间复杂度,下面这个方法是利用了完全平方数的一个性质:
a square number is 1+3+5+7+... Time Complexity O(sqrt(N))
这个性质就是说一个完全平方数是从1开始的若干连续奇数的和。
代码如下。
public class Solution {
public boolean isPerfectSquare(int num) {
for(int i = 1; num > 0; i += 2){
num -= i;
}
return num == 0;
}
}
python版本:
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
i = 1
while num > 0:
num -= i
i += 2
return num == 0
方法二:暴力求解
第一想法,直接看比这个数小的数的平方能否有等于这个数的,注意比较的范围是num/2+1,宁愿多比较一个数也不能让结果错误。
public class Solution {
public boolean isPerfectSquare(int num) {
for(int i = 1; i <= num / 2 + 1; i++){
if(i * i == num){
return true;
}
}
return false;
}
}
方法三:二分查找
注意的是left调整到mid+1,right调整到mid-1,这样交叉着才能保证left<= right的判断条件有效。
public class Solution {
public boolean isPerfectSquare(int num) {
long left = 0;
long right = num;
long mul = 0;
while(left <= right){
long mid = (right + left) / 2;
mul = mid * mid;
if(mul < num){
left = mid + 1;
}else if(mul > num){
right = mid - 1;
}else{
return true;
}
}
return false;
}
}
python版本如下,注意我使用的其实这个模板,左闭右开的区间:
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
l, r = 0, num + 1
# [l, r)
while l < r:
mid = l + (r - l) / 2
if mid * mid == num:
return True
if mid * mid < num:
l = mid + 1
else:
r = mid
return False
方法四:牛顿法
牛顿法详见:https://en.wikipedia.org/wiki/Newton%27s_method.
这个问题其实就是求f(x)=num - x ^ 2的零点。
那么,Xn+1 = Xn - f(Xn)/f'(Xn).
又f'(x) = -2x.
得Xn+1 = Xn +(num - Xn ^ 2)/2Xn = (num + Xn ^ 2) / 2Xn = (num / Xn + Xn) / 2.
即t = (num / t + t) / 2.
public class Solution {
public boolean isPerfectSquare(int num) {
long t = num / 2 + 1;
while(t * t > num){
t = (num / t + t) / 2;
}
return t * t == num;
}
}
python版本:
class Solution(object):
def isPerfectSquare(self, num):
"""
:type num: int
:rtype: bool
"""
x = num
while x * x > num:
x = (x + num / x) / 2
return x * x == num
日期
2017 年 5 月 4 日
2018 年 10 月 27 日 —— 10月份最后的周末!
2018 年 11 月 22 日 —— 感恩节快乐~
【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)的更多相关文章
- [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 else Fa ...
- 【LeetCode】383. Ransom Note 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...
- 【LeetCode】575. Distribute Candies 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- [LeetCode]367. Valid Perfect Square判断完全平方数
方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】221. Maximal Square 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...
随机推荐
- Docker 外部访问容器Pp、数据管理volume、网络network 介绍
Docker 外部访问容器Pp.数据管理volume.网络network 介绍 外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P 或 -p 参数来 指定端口映射. ...
- GlimmerHMM指南
GlimmerHMM指南 官方用户手册 GlimmerHMM是一种De novo的新基因预测软件. 新基因发现基于Generalized Hidden Markov Model (GHMM). Gli ...
- 7. Minimum Depth of Binary Tree-LeetCode
难度系数:easy /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- vs2019 16.8更新之后的 C++20 协程co_yield用法
由于搜索出来的帖子,都是老版本的实验协程,很多老的代码已经失去参考性,并且很复杂,所以就自己研究了一下. 1 #include <iostream> 2 #include <coro ...
- 学习java 7.13
学习内容: 一个汉字存储:如果是GBK编码,占用2个字节:如果是UTF-8编码,占用3个字节 汉字在存储的时候,无论选择哪种编码存储,第一个字节都是负数 字符流=字节流+编码表 采用何种规则编码,就要 ...
- act.四级
act的词源是do, 干着或干了的事情也可以叫act.action: doing sth; act: n. action, v. do; activity: busy, energetic, or占据 ...
- 【leetcode】1293 .Shortest Path in a Grid with Obstacles
You are given an m x n integer matrix grid where each cell is either 0 (empty) or 1 (obstacle). You ...
- Java事务与JTA
一.什么是JAVA事务 通俗的理解,事务是一组原子操作单元,从数据库角度说,就是一组SQL指令,要么全部执行成功,若因为某个原因其中一条指令执行有错误,则撤销先前执行过的所有指令.更简答的说就是:要么 ...
- Mybatis 批量插入
一.首先对于批量数据的插入有两种解决方案(下面内容只讨论和Mysql交互的情况) 1)for循环调用Dao中的单条插入方法 2)传一个List<Object>参数,使用Mybatis的批量 ...
- 监测linux系统负载与CPU、内存、硬盘、用户数的shell脚本
本节主要内容: 利用Shell脚本来监控Linux系统的负载.CPU.内存.硬盘.用户登录数. 一.linux系统告警邮件脚本 # vim /scripts/sys-warning.sh #!/bin ...