作者: 负雪明烛
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. nrf51822 RAM不足分析

    之前了解过STM32 的内存分配问题,对于蓝牙芯片51822的内存分配问题把项目中,遇到了.bss和.data部分超了的问题,这其实就是声明的变量和stask 及 heap的大小总和超出了单片机的RA ...

  2. The Go tools for Windows + Assembler很好玩

    我想用python做个tiny BASIC编译器.赋值和加减乘除,IF和FOR. 语法分析python有ply包,用ply.lex和ply.yacc做个计算器很简单,我已经做了. 做个解释器应该也不难 ...

  3. day01 MySQL发展史

    day01 MySQL发展史 今日内容概要 数据库演变史 软件开发架构 数据库本质 数据库中的重要概念 MySQL下载与安装 基本SQL语句 今日内容详细 数据库演变史 # 1.文件操作阶段 jaso ...

  4. 零基础学习java------35---------删除一个商品案例,删除多个商品,编辑(修改商品信息),校验用户名是否已经注册(ajax)

    一. 删除一个商品案例 将要操作的表格 思路图  前端代码 <%@ page language="java" contentType="text/html; cha ...

  5. navigationItem的leftBarButtonItem和rightBarButtonItem隐藏

    - (void)showEdit { if (不符合显示条件) { self.navigationItem.rightBarButtonItem.customView.hidden = YES; // ...

  6. zabbix之二进制安装

    #:参考官方网站 https://www.zabbix.com/documentation/4.0/manual/installation/install_from_packages/debian_u ...

  7. Java面试基础--(出现次数最多的字符串)

    题目:给定字符串,求出现次数最多的那个字母及次数,如有多个 重复则都输出. eg,String data ="aaavzadfsdfsdhshdWashfasdf": 思路: 1. ...

  8. vueAPI (data,props,methods,watch,computed,template,render)

    data Vue 实例的数据对象.Vue 将会递归将 data 的属性转换为 getter/setter,从而让 data 的属性能够响应数据变化.实例创建之后,可以通过vm.$data来访问原始数据 ...

  9. 【Office】【Excel】将多个工作薄合为一个工作薄

    前提:工作薄首行不能有合并的单元格 准备工作:将要合并的工作簿放在一个文件夹里面,文件夹中不能有乱七八糟的东西,只能有你要合并的工作薄 操作步骤:在此文件夹下创建Excel表格并打开,按下alt+F1 ...

  10. Java 多线程的一次整理

    一天没有出过家门,实属无聊,没事瞎写写 1. 基本概念 1.1 多进程和多线程的概念 程序是由指令和数据组成,指令要运行,数据要加载,指令被 CPU 加载运行,数据被加载到内存,指令运行时可由 CPU ...