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 ...
随机推荐
- ztree学习之异步加载节点(一)
ztreedemo.jsp: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...
- iOS Json转换模型库:YYModel
iOS Json转换模型库:YYModel 其实在研究这个库之前,市面上已经有很多类似的模型序列化成JSON及反序列化库(如Mantle.MJExtension)了,推荐他只是因为他高端的性能和容 ...
- 你们以为运营商只是HTTP插点广告而已么?
国内某邮件服务商,近期在某南方地区有大量客户反应登录时出错和异常,于是工作人员进行了一下跟进,发现如下: 首先,邮件服务商登陆页面为普通HTTP协议发送,提交时通过JS进行RSA加密(没错,JS的RS ...
- POJ 1185 炮兵阵地(状压DP)
炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26426 Accepted: 10185 Descriptio ...
- The P4 Language Specification v1.0.2 Parser
<p4规范>解析器部分详解 p4解析器是根据有限状态机的思想来设计的. 解析器中解析的过程可以被一个解析图(parser graph)所表示,解析图中所表示的某一个状态(或者说,在P4语言 ...
- Yii源码阅读笔记(二十)
View中应用布局和缓存内容部分: /** * Begins recording a block. * This method is a shortcut to beginning [[Block]] ...
- Xamarin Visual Studio无法debug
在Visual Studio中,Target IOS Device下拉框是禁用状态,无法选择. Xamarin论坛中有不少关于这个问题的,如下面这个帖子: http://forums.xamarin. ...
- 怎么样打印加密PDF文件
自然是解密后再打印.解密的方法,在linux下执行: pdf2ps xxx.pdf ps2pdf xxx.ps 参考资料 http://www.cyberciti.biz/faq/removing-p ...
- PHP文件操作 之往一个文件写入数据
//打开一个文件 $f = fopen($filename,'wb'); $filename:打开一个文件,不存在则自动创建,如果不能创建,说明指定的文件目录有错误 wb:写入的方式 ---- 覆盖原 ...
- 21335592 ROWS
CREATE TABLE w_big SELECT * FROM ( SEELCT * FROM w_tab UNION ALL SELECT * FROM w_tab_copy_modify ) ...