美文网首页LeetCode By Go
[LeetCode By Go 89]367. Valid Pe

[LeetCode By Go 89]367. Valid Pe

作者: miltonsun | 来源:发表于2017-09-05 10:06 被阅读5次

题目

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. 从1~num中找平方等于num的值i,找到返回true
  2. 如果i2大于num,说明num不是平方数,返回false

代码

func isPerfectSquare(num int) bool {
    
    for i := 1; i <= num; i++ {
        if num == i * i {
            return true 
        } else if num < i * i {
            return false 
        }
    } 
    
    return false 
}

相关文章

网友评论

    本文标题:[LeetCode By Go 89]367. Valid Pe

    本文链接:https://www.haomeiwen.com/subject/tkcujxtx.html