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
    Returns: True
    

    Example 2:

    Input: 14
    Returns: False

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    不知道搜到最后怎么用,其实就是得到一个区间范围,用左边或者右边

    [一句话思路]:

    二分法得到一个区间,最后判断两端就行了

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    [一刷]:

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    二分法得到一个区间,最后判断两端就行了

    [复杂度]:Time complexity: O(lgn) Space complexity: O(1)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    start end最好是long型,避免溢出

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

     [代码风格] :

    class Solution {
        public boolean isPerfectSquare(int num) {
            //bs
            long start = 1, end = num;
            while (start + 1 < end) {
                long mid = start + (end - start) / 2;
                if (mid * mid < num) {
                    start = mid;
                }
                if (mid * mid >= num) {
                    end = mid;
                }
            }
            
            //return s or e
            if (end * end == (long)num || start * start == (long)num) return true;
            return false;
        }
    }
    View Code
  • 相关阅读:
    RoIPooling、RoIAlign笔记
    ROI Align 的基本原理和实现细节
    ROI Align详解
    GIT总结
    java-变量,函数 下
    linux设置静态ip地址
    技术参考网站-网址
    python
    python
    python
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8966078.html
Copyright © 2011-2022 走看看