题意:给出一正整数,不用 sqrt函数,判断该数是否是平方数

思路:

第一种,连续奇数和为平方数

代码如下:

public class Solution {public boolean isPerfectSquare(int num) {for (int i = 1; num > 0; i += 2){num -= i;}return num == 0;}
}

第二种,用二分查找法

代码如下:

public class Solution {public boolean isPerfectSquare(int num) {int low = 0, high = 46340;while (low < high){int mid = (low + high) >> 1;if (mid * mid == num) return true;else if (mid * mid < num) low = mid + 1;else high = mid - 1;}return low * low == num;}
}

LeetCode 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. C#LeetCode刷题之#367-有效的完全平方数(Valid Perfect Square)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3869 访问. 给定一个正整数 num,编写一个函数,如果 num ...

  3. 367. Valid Perfect Square

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

  4. Project Euler 92:Square digit chains 平方数字链

    题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...

  5. Leetcode 279. Perfect Square

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  6. leetcode 367. Valid Perfect Square

    二分. bool isPerfectSquare(int num) {if (num == 0 || num == 1)return true;int start = 0;int end = num; ...

  7. C#刷遍Leetcode面试题系列连载(4): No.633 - 平方数之和

    点击蓝字"dotNET匠人"关注我哟 加个"星标★",每日 7:15,好文必达! 前文传送门: 上篇文章中一道数学问题 - 自除数,今天我们接着分析 LeetC ...

  8. Leetcode 279 完美平方数

    Type: Medium, BFS 完美平方数 题目描述: Given a positive integer n, find the least number of perfect square nu ...

  9. 279 Perfect Squares 完美平方数

    给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...) 使得他们的和等于 n.你需要让平方数的个数最少. 比如 n = 12,返回 3 ,因为 12 = 4 + 4 + 4 : ...

最新文章

  1. 51nod1601-完全图的最小生成树计数【Trie,分治】
  2. ssm使用全注解实现增删改查案例——EmpServiceImpl
  3. -9 逆序输出一个整数的各位数字_【Java编程基本功】(八)逆序输出、是否为回文数,判断星期几,升序排列...
  4. shiro启动之后页面访问不了_java:shiro入门——4
  5. WAP2.0(XHTML MP)基础介绍
  6. 一个程序员的逗逼瞬间(二)
  7. 新年快乐,送一台新款华为笔记本!
  8. 微服务启动顺序_微服务框架Demo.MicroServer运行手册
  9. 如何在Windows上制作一个包含.lib和.dll的Rust Crate包
  10. 用系统自带的SSH实现两台linux密钥认证访问
  11. 空想科学教程 (爆笑)1
  12. PSIFT:Pore Scale-invariant feature transform;毛孔尺度不变特征点
  13. Celery介绍---手机短信异步发送
  14. 腾讯Redis压轴笔记,成功入职阿里
  15. k8s集群搭建-1mater2node
  16. 融合柯西变异和反向学习的改进麻雀算法
  17. 一行代码实现微信多开
  18. JavaWeb:Tomat的安装
  19. RiskCloud双控平台(有效分享HAZOP软件篇)
  20. 为IE内核的WebBrowser控件内存泄漏所烦恼的可以考虑用Cefsharp代替它!

热门文章

  1. JS判断是否是移动设备进行http链接重定向
  2. MySQL数据库性能优化--SQL优化
  3. jQuery队列控制方法详解queue()/dequeue()/clearQueue()
  4. mysql 路由_mysql 命令大全
  5. python安装流程-Python安装与卸载流程
  6. python做直方图-python OpenCV学习笔记实现二维直方图
  7. python语言中文社区-python中用中文
  8. python恶搞表情包-用 Python 把你的朋友变成表情包
  9. python3.7安装包-Python3.7.6下载
  10. python制作工资计算器-Python实现扣除个人税后的工资计算器示例