首页 > 学院 > 开发设计 > 正文

LeetCode -- Valid Perfect Square

2019-11-08 02:11:42
字体:
来源:转载
供稿:网友
题目描述: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: 16Returns: TrueExample 2:Input: 14Returns: 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;    }}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表