题解 | #求平方根#

求平方根

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:35
点赞 评论 收藏
分享
自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务