当前位置:网站首页>Day367: valid complete square

Day367: valid complete square

2022-06-23 01:22:00 Shallow look

problem : Effective complete square number

Given a positive integer num , Write a function , If num It's a complete square , Then return to true , Otherwise return to false .

Advanced : Don't Use any built-in library functions , Such as sqrt .

source : Power button (LeetCode)

Train of thought : Brute force ergodic solution

For less than num Integer of from 1 To traverse the , Determine whether the square is equal to num, If equal to, return true, Otherwise return to false.

class Solution {
    
    public boolean isPerfectSquare(int num) {
    
        boolean result = false;
        if (num==1) result = true;
        for(int i=2; i < num/2+1; i++){
    
            if(i*i==num){
    
                result = true;
                break;
            }
        }
        return result;
    }
}

shortcoming : When the integer is too large , Time limit exceeded .

Train of thought two : Dichotomy

  1. The root of a perfect square number x, Must be satisfied with 1 ≤ x ≤ n u m 1 \le x \le num 1xnum, We use this interval as the search interval .
  2. left Start from the left side of the section ,right Start from the right side of the section , Calculate the median m i d = ( l e f t + r i g h t ) 2 mid=\frac{(left+right)}{2} mid=2left+right)
  3. Perfect square heel x Must satisfy one of the three relationships , m i d = = x , m i d < x , m i d > x mid ==x ,mid <x, mid>x mid==x,mid<x,mid>x, So as left or right Update conditions for .
  4. The dichotomy is compared to the violent solution , The time complexity is reduced to some extent .
class Solution {
    
    public boolean isPerfectSquare(int num) {
    
        int left = 0;
        int right = num;
        boolean result = false;
        while (left <= right){
    
            int mid = (left + right)/2;
            long mid_square = (long)mid*mid;
            if(mid_square < num){
    
                left = mid + 1;
            }else if(mid_square > num){
    
                right = mid - 1;
            }
            else{
    
                result = true;
                return result;
            }
        }
        return result;
    }
}

Train of thought three : Newton method

Newton's method is an approximate method to solve the zero point of a function , The iterative idea is as follows .
 Insert picture description here
For the function f ( x ) f(x) f(x), Take any starting point ( x 0 , f ( x 0 ) ) (x0,f(x0)) (x0,f(x0)), Do the function at this point f ( x ) f(x) f(x) The tangent of , Tangent and x Axis intersection point x 1 x1 x1 Is the abscissa of the point to be updated next time , comparison x 0 x0 x0 for , x 1 x1 x1 Closer to zero , After many iterations , It can be approximated as point x 1 x1 x1 Approaching the zero of a function .

Ideas :
In this case f ( x ) = x 2 − n u m f(x)=x^2-num f(x)=x2num
Update formula x i + 1 = ( x i + n u m / x i ) / 2 x_{i+1}=(x_i+num/{x_i})/2 xi+1=(xi+num/xi)/2
The end condition : We think that when the results of two adjacent iterations are similar , It can be considered close enough to zero .

Code :

class Solution {
    
    public boolean isPerfectSquare(int num) {
    
        double x0 = num;
        double x1 = 0;
        while (true){
    
            x1 = (x0 + num/x0)/2;
            if (x0 - x1 < 1e-1)
                break;
            x0 = x1;
        }
        x1 = (int)x1; 
        return x1*x1 == num;
    }
}

Reference resources : Try to answer the official questions

原网站

版权声明
本文为[Shallow look]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220918142193.html