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 ...
随机推荐
- html ul 里 能放其他标签吗,比如h3、p?
按标准的话,ul 里面应该只能放li,不过看见同事有的放了h3.p这些,这样到底可不可以? <ul>里面只能放<li>.但是<li>里面可以放<h*> ...
- 八、job管理
查看用法: [root@super65 ~]# salt-run -d|grep jobs'jobs.active:' #查看当前执行的job Return a report on all activ ...
- Leetcode | Valid Sudoku & Sudoku Solver
判断valid,没有更好的方法,只能brute force. class Solution { public: bool isValidSudoku(vector<vector<char& ...
- yii2 debug工具条不出现
UrlManger美化后,debug工具条不出现,禁用UrlManager出现
- POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25904 Accepted: 7682 Descr ...
- myBatis应用
最近项目中使用myBatis(iBatis),所以目前对所遇的一些问题及有些模糊的地方在这里标注一下. 首先mybaits是一个“半自动化”的ORM框架. 需要使用的jar包:mybatis-3.0. ...
- [ZZ] Understanding 3D rendering step by step with 3DMark11 - BeHardware >> Graphics cards
http://www.behardware.com/art/lire/845/ --> Understanding 3D rendering step by step with 3DMark11 ...
- HTTP 笔记与总结(8)HTTP 与内容压缩
以环球网的一篇新闻为例,抓包图: (Powered-By-ChinaCache:HIT from 060120b3g7.16 表示当前页面不是来自环球网的主服务器,而是来自中国的缓存服务器节点,HIT ...
- 一些App的User-Agent
天猫 Mozilla/5.0 (Linux; U; Android 4.4.4; zh-cn; MI 2C Build/KTU84P) AppleWebKit/537.36 (KHTML, like ...
- json+mvc
返回JsonResult序列化的Json对象 public class MyController : Controller { // 必须返回ActionResult类型 public ...