题解 | #求平方根#
求平方根
http://www.nowcoder.com/practice/09fbfb16140b40499951f55113f2166c
定义一个整数ans,循环求这个数的平方ansans,与x进行比较,直接ansans大于x时退出,返回ans-1即可
class Solution {
public:
/**
*
* @param x int整型
* @return int整型
*/
int sqrt(int x) {
// write code here
int ans = 0;
while(ans * ans <= x)
{
ans++;
}
return ans-1;
}
};
查看14道真题和解析
哔哩哔哩公司氛围 721人发布