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. Word 2010 给公式添加序号

    在写paper的时候,我们常常要给公式编号,难道我们要在公式和最右边的序号之间疯狂按空格键吗,当然不是,我们可以有更高效的方法来完成. 首先我们插入一个1x3的表格,然后调节首尾两个格子的大小,借助标 ...

  2. #undef

    #undef 是在后面取消以前定义的宏定义 该指令的形式为 #undef 标识符 其中,标识符是一个宏名称.如果标识符当前没有被定义成一个宏名称,那么就会忽略该指令. 一旦定义预处理器标识符,它将保持 ...

  3. [转]Visual Studio 实用扩展推荐

    本文转自 http://www.cnblogs.com/stg609/p/3726898.html Visual Studio 拥有非常不错的可扩展性,在之前的文章中,我也给大家示范了如何进行编辑器的 ...

  4. Description of security events in Windows 2003/7/2008

    https://technet.microsoft.com/library/cc163121.aspx#EKH https://support.microsoft.com/en-us/kb/97751 ...

  5. HttpRequestDeviceUtils

    import javax.servlet.http.HttpServletRequest;public class HttpRequestDeviceUtils { /**Wap网关Via头信息中特有 ...

  6. ci调用application/views下的css,js,图片资源出现You don't have permission to access CodeIgniter on this server解决

    原因是view文件下面有个.htaccess文件,里面写的是 Deny from all     //拒绝所有请求 自己本地测试的话,就直接去掉,放到服务器就指定application/views文件 ...

  7. jQuery EasyUI DataGrid Checkbox 数据设定与取值

    纯粹做个记录,以免日后忘记该怎么设定. 这一篇将会说明两种使用 jQuery EasyUI DataGrid 的 Checkbox 设定方式,以及在既有数据下将 checked 为 true 的该笔数 ...

  8. fastjson生成json时Null属性不显示

    举个例子 生成JSON代码片段 Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonM ...

  9. laravel 自定义函数 使用

    1.创建app/helpers.php 2.注册路径 { ... "autoload": { "files": [ "app/helpers.php& ...

  10. jquery autocomplete插件结合ajax使用

    <%@ page isELIgnored="false"%> <%@ page contentType="text/html; charset=UTF- ...