`

LeetCode 69 - Sqrt(x)

 
阅读更多

Implement int sqrt(int x).

Compute and return the square root of x.

Solution 1:

Binary Search. m*m <= x < (m+1)*(m+1)

public int sqrt(int x) {
    if(x <= 1) return x;
    int l = 1, h = x / 2 + 1;
    while(l <= h) {
        int m = (l + h) / 2;
        if(x/m>=m && x/(m+1)<m+1) {
            return m;
        } else if(x/m < m) {
            h = m-1;
        } else {
            l = m+1;
        } 
    }
    return l;
}
 

Solution 2:

Also Binary Search. m*m <= x < (m+1)*(m+1)

public int sqrt(int x) {
    if(x <= 1) return x;
    int l = 1, h = x / 2 + 1;
    while(l <= h) {
        int m = (l + h) / 2;
        if(x/m < m) {
            h = m-1;
        } else if(x/m > m){
            l = m+1;
        } else {
            return m;
        }
    }
    return (l+h)/2;
}

 

Solution 3:

Newton's Method.

public int sqrt(int x) {
    double eps = 0.001;
    double last = x / 2.0;
    do {
        last = (last + x / last) / 2.0;
    } while(last*last - x > eps);
    return (int)last;
}

 

分享到:
评论

相关推荐

    leetcode答案-LeetCode69:力扣69

    69 题 1.题目要求 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 ...

    leetcode343-LeetCode:LeetCodeJava解题

    第 343 章LeetCode解题笔记 代码请于src目录笔记见笔记目录下文档README 不更新了,直接提交。。。...69.Sqrt(x) 48.旋转图像 2017.10.30 46.排列 2017.10.29 40.组合和II 39.组合和112.路径和 201

    leetcode530-LeetcodeSolution:Leetcode的解决方案

    69.Sqrt(x); 300.LongestIncreasingSubsequence。 338. 计数位数419. 棋盘中的战舰461. 汉明距离476.数字补码500.键盘排93.恢复IP地址344.反向字符串463.岛屿周长485.最大连续数513. 查找左下树值406.按高度重构队列...

    leetcode : 69. x 的平方根

    实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

    Leetcode-Algorithm-Exercise

    Leetcode算法练习 Leetcode算法练习 ...MaximumSubarray 58_LengthOfLastWord 66_PlusOne 67_AddBinary 69_Sqrt(x) 70_ClimbStairs 83_RemoveDuplicatesFromSortedList 88_MergeSortedArray 100_SameT

    leetcode双人赛-Leetcode:这个repo在我的java和python解决方案和描述中记录了Leetcode

    Sqrt(x) int mid long prod = mid * mid仍會overflow 要改成 long mid宣告才行 联合查找中的路径压缩 private int find(int x) { if (parent[x] == x) { return parent[x]; } parent[x] = find(parent[x]); // path ...

    leetcode 69: x的平方根

    原题描述 leetcode 69 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2...

    leetcode-js:算法和数据结构是一个程序员的灵魂,LeetCode JavaScript TypeScript 题解

    69.x 的平方根 (Sqrt(x)) 70.爬楼梯 (Climbing Stairs) 83.删除排序链表中的重复元素 (Remove Duplicates from Sorted List) 88.合并两个有序数组 (Merge Sorted Array) 100.相同的树 (Same Tree) 104.二叉树的最大...

    LeetCode最全代码

    [Progress](https://img.shields.io/badge/progress-468%20%2F%20468-ff69b4.svg) Up to date (2016-12-18), there are `447` Algorithms / `13` Database / `4` Shell / `4` Draft questions on [LeetCode Online ...

    leetcode2sumc-Leetcode_questions:Leetcode_questions

    69.Sqrt(x) (c++:二元除法) 70.Climbing Stairs(c++:Dynamic Programming) 83.从排序列表中删除重复项(c++) 88.Merge Sorted Array(c++) 00 94.Binary Tree Inorder Traversal(c++:tree traversal inorder) 100.Same...

    LeetCodeForCCC

    69.Sqrt(X)-简单 704.Binary Search-简单 35.搜索插入位置-简单 34.在排序数组中查找元素的第一个和最后一个位置-中 &gt;更新: 只能使用“如果if(nums [mid] &lt;target)left = mid + 1”来刷新范围。 并返回...

Global site tag (gtag.js) - Google Analytics