LeetCode Collection (17) - 69 questions Sqrt (X)

problem

Implement int sqrt(int x). 

 Compute and return the square root of x, where x is guaranteed to be a non-negative integer. 

 Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. 

 Example 1: 


Input: 4
Output: 2


 Example 2: 


Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since 
             the decimal part is truncated, 2 is returned.

复制代码

translation:

Achieve int sqrt (int x). Computes and returns the square root of x, where x is a non-negative integer guaranteed. Since the return type is an integer, decimal truncates and returns only the integer part. Example 1: Input: Output 4: 2 Example 2: Input: Output 8: 2 Description: 2.82842 ... is the square root of 8, the number of childhood is cut off, returned 2.


Problem-solving ideas

This question is to find the square root of a number is the current number, if it is a decimal, the return value is discarded decimals. We can traverse the way to judge is not, then here need to consider cross-border issues, in fact, may not be concerned about, after all, can be drawn out of range of the upper limit of the square root of a number, you can avoid this problem. In addition to traverse, we can also use the built-in java Math class to solve, is the easiest. In addition, this question is to find value, but is looking for a value within a specific range, you can think about whether you can use dichotomy brief query time.

Problem-solving approach

  1. According to our idea to edit the code as follows

    if (x <= 0) {
            return 0;
        }
    
        for (int i =x/2+1; i>=0; i=i/2) {
            long result = 1L*i * i;
            if (result == x) {
                return i;
            }
            if (result > x) {
                return i - 1;
            }
        }
        return 0;
    复制代码

    Time Complexity : The program with the circulation m so that f (n) = (n / 2) = n; so O (f (n)) = O (n / 2), i.e., T (n) = O (n )

    Space complexity : The program uses the extra space is not used, so the space complexity is O (n) = O (1 );

  2. Using dichotomy, as follows

    if (x <= 0) {
            return 0;
        }
        int start = 0;
        int end = x;
        while (start <= end) {
            int index = (start + end) / 2;
            long sum = 1L * index * index;
            if (sum > x) {
                end = index - 1;
            } else {
                start = index + 1;
            }
        }
        return end;
    复制代码

    Time Complexity : The program cycle with m so f (n) = (logn) = n; so O (f (n)) = O (logn), i.e., T (n) = O (logn )

    Space complexity : The program uses the extra space is not used, so the space complexity is O (n) = O (1 );

  3. Math class borrow, as follows

     if (x <= 0) {
            return 0;
        }
    
        return (int)Math.sqrt(x);
    复制代码

    Time Complexity : The program cycle with m so f (n) = (1) = n; so O (f (n)) = O (1), i.e., T (n) = O (1 )

    Space complexity : The program uses the extra space is not used, so the space complexity is O (n) = O (1 );

to sum up

Approximate solution of this problem as above complaint, specifically in the range, but also in order, we naturally think of dichotomy to simplify traversal, since this question was recently required minimum, so when end--, the large value on become to a minimum, just good meet.

Reproduced in: https: //juejin.im/post/5cfa8a2e6fb9a07efb69777d

Guess you like

Origin blog.csdn.net/weixin_34192732/article/details/91433002