zoukankan      html  css  js  c++  java
  • 367. 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
    Output: true
    

    Example 2:

    Input: 14
    Output: false

    Approach #1:

    class Solution {
    public:
        bool isPerfectSquare(int num) {
            long long l = 0, r = num;
            while (l <= r) {
                long long m = l + (r - l) / 2;
                long long flag = m * m;
                if (flag == num) return true;
                if (flag < num) l = m + 1;
                if (flag > num) r = m - 1;
            }
            return false;
        }
    };
    

      

    Runtime: 0 ms, faster than 100.00% of C++ online submissions for Valid Perfect Square.
    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    UVA
    BZOJ 2330: [SCOI2011]糖果
    人类的殒落与扬升
    算法笔记2
    算法笔记
    回溯法
    贪心法
    动态规划
    分治与递归
    计算机图形学 补 光线跟踪
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9907378.html
Copyright © 2011-2022 走看看