Valid Perfect Square

来源:互联网 发布:java监控服务状态 编辑:程序博客网 时间:2024/05/01 23:30

Valid Perfect Square

description

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

thinking

使用分数指数次幂的方法,求num的0.5次幂,实际上就是做了sqrt

solution

class Solution {  public:      bool isPerfectSquare(int num)     {          return (int)(pow(num, 0.5)) == pow(num, 0.5) ? true : false;      }  };  
原创粉丝点击