码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Valid Perfect Square

时间:2017-08-30 18:35:21      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:elm   style   creat   write   als   个数   false   edit   and   

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

Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.

不使用sqrt判断一个数是否是完全平方数,

class Solution {
public:
    bool isPerfectSquare(int num) {
        for (int i = 1; i <= num / i; i++)
            if (i * i == num)
                return true;
        return false;
    }
};
// 0ms

 

[LeetCode] Valid Perfect Square

标签:elm   style   creat   write   als   个数   false   edit   and   

原文地址:http://www.cnblogs.com/immjc/p/7454540.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!