LeetCode -- Valid Perfect Square

来源:互联网 发布:手机淘宝客户端软件 编辑:程序博客网 时间:2024/05/01 03:16
题目描述:


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;    }}


1 0
原创粉丝点击