IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    [原]LeetCode -- Valid Perfect Square

    csharp25发表于 2017-02-19 22:24:31
    love 0
    题目描述:


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


    Note: Do not use any built-in library function such as sqrt.


    Example 1:


    Input: 16
    Returns: True
    Example 2:


    Input: 14
    Returns: False


    本题属于数学分类。
    思路:


    任意完全平方数可通过1+3+5...+K的和得到。


    实现代码:


    public class Solution {
        public bool IsPerfectSquare(int num) {
            var start = 1;
    	
        	while(num > 0){
        		num -= start;
        		start += 2;
        	}
        	
        	return num == 0;
        }
    }




沪ICP备19023445号-2号
友情链接