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
    

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

    Show Company Tags
    Hide Tags
     
    public class Solution {
        public boolean isPerfectSquare(int num) {
            long start = 0;
            long end = num/2;
            if(num==1)return true;
            while(start<=end)
            {
                long mid = (long)start + (end-start)/2;
                long val = (long)mid * mid;
                if(val==num) return true;
                else if(val<num)
                {
                    start = mid+1;
                }
                else
                {
                    end = mid-1;
                }
            }
            
            return false;
        }
    }
  • 相关阅读:
    安装solr
    Linux下安装mysql
    SQL JOIN
    SQL之TCL
    SQL之DCL
    SQL之DML
    SQL之DDL
    Mysql 常用查询语句
    Java-Poi 读取excel 数据
    工作中的第一份LoadRunner脚本
  • 原文地址:https://www.cnblogs.com/hygeia/p/5774930.html
Copyright © 2011-2022 走看看