[LeetCode] 69. Sqrt(x)(C++)

題目

Given a non-negative integer x, compute and return the square root of x.

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

Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.

Example 1:

1
2
Input: x = 4
Output: 2

Example 2:

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

Constraints:

  • 0 <= x <= 231 - 1

解題方向

連分數法求根號值,可以參考李永樂老師的影片 可以得知 S=a^2+b 中的 a 會等於題目所求的答案

參考程式碼

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class Solution {
public:
    int mySqrt(int x) {
        long count=0;
        int temp;
        while(true){
            if(count*count>x){
                return temp;
            }
            temp=count;
            count++;
        }
        return {};
    }
};
Never Too Late To Start
使用 Hugo 建立
主題 StackJimmy 設計