方法有很多,我觉得比较容易记住的是两个,一个是二分法,在1-num/2中寻找目标数

另一个是数学方法:

public boolean isPerfectSquare(int num) {
/*
有很多方法,二分法,搜索法等等
最简单的方法需要知道一个数学定理
完全平方数等于奇数序列相加,1=1,4=1+3,9=1+3+5
*/
if (num<=0) return false;
int i=1;
while (num>0){
num-=i;
i+=2;
if(num==0) return true;
}
return false;
}

[LeetCode]367. Valid Perfect Square判断完全平方数的更多相关文章

  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. 367. Valid Perfect Square判断是不是完全平方数

    [抄题]: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  4. Leetcode 367. Valid Perfect Square

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

  5. 【easy】367. Valid Perfect Square 判断是不是平方数

    class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...

  6. 367. Valid Perfect Square

    原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...

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

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

  8. 【LeetCode】367. Valid Perfect Square 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:完全平方式性质 方法二:暴力求解 方法三:二 ...

  9. 【leetcode】367. Valid Perfect Square

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

随机推荐

  1. 01-Python里字符串的常用操作方法--replace()函数

    1. replace()  函数 作用: 替换字符串 语法: replace('需要替换的子串', '新的子串', '替换的次数(可不传)') # 当替换次数不传时,默认全部替换 实例一: mystr ...

  2. python之迭代器,生成器小结

    1.凡是可作用于for循环的对象都是Iterable类型: 2.凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列: 3.集合数据类型如list.dict.str等 ...

  3. python之汉诺塔

    # -*- coding: utf-8 -*- def move(n, a, b, c): if n==1: print(a,'==>',c)#只有一块的时候直接从A到C即可 else: mov ...

  4. vue获取浏览器地址栏参数(?及/)路由+非路由实现方式

    1.? 参数 浏览器参数形式:http://javam4.com/m4detail?id=1322914793170014208 1.1.路由取参方式 this.$route.query.id 前端跳 ...

  5. js创世纪--刨根问底原型和原型链

    原型和原型链 看图说话: 1.对象内部具有[[Prototype]]属性,该属性不可直接访问,浏览器通过__proto__(两条'_')可以让用户读写该内部属性,最重要的是,该属性指向创建本对象的原型 ...

  6. 安装spyder记录

    sudo apt-get install spyder 报错:ERROR: Could not find a version that satisfies the requirement pyqt5& ...

  7. 用java以正确的姿势刷CSP

    许多程序算法考试中,用java是个不错的选择,它几乎实现了所有c++能实现的,所以越来越受Acmer的欢迎.总结一下用到的一些技巧和方法.更多关于csp的可参考海岛blog|皮卡丘 1. 输出 规格化 ...

  8. 第7章 Python类型、类、协议 第7.1节 面向对象程序设计的相关知识

    Python被视为一种面向对象的语言,在介绍Python类相关的内容前,本节对面向对象程序设计相关的概念进行简单介绍. 一.    类和对象(实例) 在面向对象的程序设计(OOP)过程中有两个重要概念 ...

  9. 半夜删你代码队 Day3冲刺

    一.每日站立式会议 1.站立式会议 成员 昨日完成工作 今日计划工作 遇到的困难 陈惠霖 了解相关网页设计 了解相关网页设计 无 侯晓龙 写了第一个例子 尝试写第一个实例子 无 周楚池 学习 与余金龙 ...

  10. Jmeter(5)JSON提取器

    Jmeter后置处理器-JSON提取器 JSON是一种轻量级数据格式,以"键-值"对形式组织数据. JSON串中{}表示对象,[]表示对象组成的数组.对象包含多个"属性& ...