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的更多相关文章

  1. [LeetCode] 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. Leetcode Perfect Square

    这道题首先想到的算法是DP.每个perfect square number对应的解都是1.先生成一个n+1长的DP list.对于每个i,可以用dp[i] = min(dp[j] + dp[i-j], ...

  4. Valid Perfect Square

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

  5. 【leetcode】367. Valid Perfect Square

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

  6. [Swift]LeetCode367. 有效的完全平方数 | Valid Perfect Square

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

  7. 367. Valid Perfect Square

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

  8. leetcode367--Valid Perfect Square

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

  9. [leetcode]367. Valid Perfect Square验证完全平方数

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

随机推荐

  1. [转]Redis集群的配置

    一:memcache 和 Redis 对比总结 [memecache 特点] 1:速度最快(没有自测,但网上有详细的测试用例) 2:支持水平扩展,可以任意添加节点 [redis 特点] 1:速度没有m ...

  2. C++ sort vector<vector<int> > or vector<MyClass> 容器的排序

    C++的STL中提供了很强大的排序函数sort,可以对任意数组,结构体及类进行排序,下面我们先来看最简单的数组排序.默认的升序排列,我们也可以在后面加上less或greater来告诉编译器我们想要的排 ...

  3. lightning mdb 源代码分析(2)

    本系列前一篇已经分析了lightningmdb的整体架构和主要的数据结构.本文将介绍一下MMAP原理以及lmdb中如何使用它. 1. Memory Map原理 内存映射文件与虚拟内存有些类似,通过内存 ...

  4. hdu Proud Merchants

    此题是一个背包的题目,刚开始我并没有作任何的排序工作,所以出来的结果总是错的,仔细想想也确实是不对的,因为q[i]会限制dp[i]的值的变化.虽然我知道要按照某个量进行排序,对原数据进行处理,但是实在 ...

  5. Css - 文本溢出处理

    http://blog.163.com/yinwei_666/blog/static/2036157320101113102733794/ overflow: hidden;-o-text-overf ...

  6. Why Consumer Hardware Start-ups Fail

    今年看到一篇文章还是很受启发. If you have the guts to start selling what you believe in, customers who share your ...

  7. 批量硬关联本地AD帐号与Office云端帐号

    世纪互联给的方案, 说只能一个一个做硬匹配, 把我吓尿了. 我整个简单的, 还能批量做. 1. 将本地域中所有用户的这两个属性导出. Get-ADUser -Filter * -SearchBase ...

  8. CSS系列:表达式(Expression)`淘汰`

    概述 CSS表达式是动态设置CSS属性的强大(但危险)方法.Internet Explorer从第5个版本开始支持CSS表达式. 兼容性 expression方法在其它浏览器中不起作用,因此在跨浏览器 ...

  9. Apache POI使用详解

    Apache POI使用详解 1.POI结构与常用类 (1)POI介绍 Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案 ...

  10. ios-指纹识别

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { //1. 判断系统版本 if ( ...