zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    第32周二
    第32周一
    第31周日
    第31周六
    第31周五
    第31周四
    第31周三
    C++中this指针的使用方法.
    ArcPad 10 的安装部署
    UEditor用法
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/6391424.html
Copyright © 2011-2022 走看看