当前位置:网站首页>LeetCode_ 69 (square root of x)

LeetCode_ 69 (square root of x)

2022-07-05 14:13:00 ***

Title Description :
Give you a nonnegative integer x , Calculate and return x Of Arithmetical square root .

Because the return type is an integer , Results are retained only Integral part , The decimal part will be Give up .

Be careful : No built-in exponential functions and operators are allowed , for example pow(x, 0.5) perhaps x ** 0.5 .

Example 1:
Input :x = 4
Output :2

Example 2:
Input :x = 8
Output :2 explain :8 The arithmetic square root of is 2.82842…, Because the return type is an integer , The decimal part will be removed .

Tips :
0 <= x <= 231 - 1

class Solution {
    
    public int mySqrt(int x) {
    
        int ans=-1;
        if(x==0)return 0;
        for (int i = 1; i <=x; i++) {
    
            if(x/i==i){
    
                ans=i;
                break;
            }
            else if((x/i)>i&&(x/(i+1))<(i+1)){
    
                ans=i;
                break;
            }
        }
        return ans;
    }
}
原网站

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

随机推荐