首页 > 代码库 > [LeetCode]69 Sqrt(x)
[LeetCode]69 Sqrt(x)
https://oj.leetcode.com/problems/sqrtx/
http://blog.csdn.net/linhuanmars/article/details/20089131
public class Solution { public int sqrt(int x) { // Assume x >= 0 if (x == 0) return 0; return s(x, 0L, (long)x); } private int calc(int x, long low, long high) { // Why using long. // is because we might calculate (MAX_VALUE / 2 + somenumber) ^ 2 if (low > high) { // This is the stop point. return (int)high; } long mid = (low + high) / 2; long y = mid * mid; if (x == y) { return (int)mid; } else if (x > y) { return calc(x, mid + 1, high); } else { return calc(x, low, mid - 1); } } }
[LeetCode]69 Sqrt(x)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。