Interview Check If n Is A Perfect Square
Check if a given number is a perfect square with only addition or substraction operation.
eg. 25 returns true; 19 returns false.
Perfect square number 有一个特性,比如0,1,4,9,16,25 他们之间的间隔分别是1,3,5,7,9,每次间隔都是i+2.
所以每次往下减i, i 更新成i+2. 看最后结果是否为0即可。
import java.util.*;
public class isPerfectSquare{
public static void main(String [] args){
int num1 = 0;
int num2 = 1;
int num3 = 25;
int num4 = 19;
System.out.println(isPerfectSquare(num1));
System.out.println(isPerfectSquare(num2));
System.out.println(isPerfectSquare(num3));
System.out.println(isPerfectSquare(num4));
}
private static boolean isPerfectSquare(int n){
//1,4,9,16,25
//1+3+5+7+9
if(n<0){
return false;
}
int i = 1;
while(n>0){
n-=i;
i+=2;
}
if(n == 0){
return true;
}else{
return false;
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Interview Check If n Is A Perfect Square的更多相关文章
- [LeetCode] 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 Perfect Square
这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...
- 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 e ...
- [Swift]LeetCode367. 有效的完全平方数 | Valid Perfect Square
Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...
- 367. Valid Perfect Square
原题: 367. Valid Perfect Square 读题: 求一个整数是否为完全平方数,如1,4,9,16,……就是完全平方数,这题主要是运算效率问题 求解方法1:812ms class So ...
- leetcode367--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 ...
随机推荐
- u盘文件系统故障的修复方法
比如U盘挂载的文件系统是/dev/sda1,且文件系统有故障(FAT: Filesystem error) 修复U盘文件系统故障 sudo dosfsck -v -a /dev/sda1
- Vim 学习资料
VIM中文手册 简明 Vim 练级攻略 所需即所获:像 IDE 一样使用 vim
- PCL 1.4.0 VS 2010 Configuration
Open VS2010, create a new project, then open Property Manager, double-click Microsoft.Cpp.win32.user ...
- 【产品更新】EVC8013 硬件更新!
EVC8013 三合一磁耦合隔离转换器(USB转RS-232 / RS-485 / RS-422 ),迎来一次硬件大幅度升级,升级不加价! 本次升级主要有以下几个方面: 1.采用第二代金升阳 3000 ...
- 关于Message目录的设定
Yii2 默认了两个message 目录,一个是 yii 和app ,所以如果想自定义message目录,要在自己配置文件中覆盖 i18n的设定,请注意,不能使用 *,要使用 app,才能生效
- windows下Ruby开发环境搭建
一.下载 下载RubyInstaller 二.安装 按照提示安装 安装完毕后,在cmd命令窗口,输入:ruby -v 查询rbuy版本:输入gem -v 查询gem版本 三.Ruby插件(Redis插 ...
- 【转】各版本IIS下ASP.net请求处理过程区别
原文地址:http://www.cnblogs.com/fsjohnhuang/articles/2332074.html ASP.NET是一个非常强大的构建Web应用的平台,它提供了极大的灵活性和能 ...
- javascript Array类
Array类 toString()方法和valueOf()方法,返回特殊的字符串.该字符串是通过对每项调用toString()方法,然后用逗号把它们连接在一起构成的.例如,对具有项"red& ...
- 使用photoshop,把图片背景变成透明
鄙人使用的是photoshop CS6,win7系统,好了废话不多说,我们开始吧 1.打开photoshop,选择一个要编辑的图片 2.在右下角的图层面板上用鼠标左键快速双击背景图层为图片解锁 3.在 ...
- Memcache 提高缓存命中率
最近手上某个项目跟新代码,新的代码里大量采用memcahce作为缓存.所以开始深入了解memcache的内存分配策略.以前就听说有个PHP写的memcache监控脚本,在网上搜索了一下,果断下载下来用 ...