作者: 负雪明烛
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)的更多相关文章

  1. [LeetCode] 367. Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  2. [leetcode]367. Valid Perfect Square验证完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. Leetcode 367. Valid Perfect Square

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  4. 【LeetCode】383. Ransom Note 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 [LeetCo ...

  5. 【LeetCode】575. Distribute Candies 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  6. 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. [LeetCode]367. Valid Perfect Square判断完全平方数

    方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数 另一个是数学方法: public boolean isPerfectSquare(int num) { /* 有很多 ...

  8. 【LeetCode】136. Single Number 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...

  9. 【LeetCode】221. Maximal Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址: https://leet ...

随机推荐

  1. python(3)跳过第一行(多行)读入数据

    查了下资料,常见两种办法,一是设置行号,再者是利用python自带的itertools工具. 这里推荐一种新的方法,直接使用readline()函数就搞定. 示例: 创建一个文本文件,内容如下: 1 ...

  2. 12 — springboot集成JPA — 更新完毕

    1.什么是jpa? 一堆不想整在这博客里面的理论知识.这些理论玩意儿就应该自行领悟到自己脑海里 1).JPA & Spring Data JPA 1.1).JPA JPA是Java Persi ...

  3. mysql数据查询语言DQL

    DB(database)数据库:存储数据的'仓库',保存了一系列有组织的数据 DBMS(Database Management System)数据库管理系统:用于创建或管理DB SQL(Structu ...

  4. day15 内置函数和模块

    day15 内置函数和模块 1.三元表达式 代码如下: x = 1 y = 2 res = 'ok' if x > y else 'no' print(res) 输出结果:no 2.内置函数:重 ...

  5. 14. GLIBCXX_3.4.9' not found - 解决办法

    在Linux中安装交叉编译器arm-linux-gcc 4.4.3,然后编译mini2440内核出错: /usr/lib/libstdc++.so.6: version GLIBCXX_3.4.9' ...

  6. spring boot 之监听器ApplicationListener

    监听器ApplicationListener 就是spring的监听器,能够用来监听事件,典型的观察者模式.ApplicationListener和ContextRefreshedEvent一般都是成 ...

  7. IntentFilter,PendingIntent

    1.当Intent在组件间传递时,组件如果想告知Android系统自己能够响应那些Intent,那么就需要用到IntentFilter对象. IntentFilter对象负责过滤掉组件无法响应和处理的 ...

  8. SpringBoot(3):SpringData 数据访问

    一. 简介 Spring Data是一个用于简化数据库访问,并支持云服务的开源框架:其主要目标是 使得对数据的访问变得方便快捷.对于数据访问层,无论是 SQL(关系型数据库) 还是 NOSQL(非关系 ...

  9. 【编程思想】【设计模式】【创建模式creational】建造者模式builder

    Python版 https://github.com/faif/python-patterns/blob/master/creational/builder.py #!/usr/bin/python ...

  10. webpack配置(vue)

    Vue-loader Vue-loader 是一个加载器,能把 .vue 文件转换为js模块. Vue Loader 的配置和其它的 loader 不太一样.除了将 vue-loader 应用到所有扩 ...