题目传送地址:​​https://leetcode.cn/problems/sqrtx/submissions/​

运行效率:

Leetcode69. x 的平方根_i++


代码如下:

class Solution {
public static int mySqrt(int x) {
for(int i=0;i<=x;i++){
long powNum = (long) i * i; //这里要小心数字溢出的情况,所以用long类型
if(powNum>x){
return i-1;
}
}
return x;
}
}