`
zhang_xzhi_xjtu
  • 浏览: 536776 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

[leetcode] SqrtX

阅读更多
package leetcode;

/**
* <pre>
* Implement int sqrt(int x).
*
* Compute and return the square root of x.
* </pre>
* */
public class SqrtX {
    public static class Solution {
        public int sqrt(int x) {

            long start = 0;
            long end = (long) Integer.MAX_VALUE + 1;

            //start*start<=x<end*end
            while (true) {
                long mid = start + (end - start) / 2;

                if (mid * mid > x) {
                    end = mid;
                    continue;
                }

                if ((mid + 1) * (mid + 1) > x)
                    return (int) mid;

                start = mid;
            }
        }
    }

}
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics