首页 > 代码库 > Valid Perfect Square
Valid Perfect Square
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 public class Solution { 2 public boolean isPerfectSquare(int num) { 3 int low = 1, high = num; 4 5 while (low <= high) { 6 int mid = low + (high - low) / 2; 7 8 if (mid == num / mid && num % mid == 0) return true; 9 else if (mid > num / mid) high = mid - 1; 10 else low = mid + 1; 11 } 12 return false; 13 } 14 }
Valid Perfect Square
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。