leetcode 69 Sqrt(x) ---java

Implement int sqrt(int x).

Compute and return the square root of x.

求一个数的平方根。

其实就是一个二分查找。

有两种查找方式

1、就是找到最大int值,然后从0到max,二分查找。

2、直接针对x,从0到x,二分查找。

public class Solution {
    public int mySqrt(int x) {
        int max = 46340;
        int flag = max/2;
        if( x >= max*max )
            return max;
        int start = 0,end = max;
        while(true){
            if( x >= flag*flag ){
                if( x < (flag+1)*(flag+1) )
                    return flag;
                else{
                    start = flag;

                }
            }else{
                if( x >= (flag-1)*(flag-1))
                    return flag-1;
                else{
                    end = flag;
                }
            }
            flag = start+(end-start)/2;
        }
    }
}
时间: 2024-10-28 23:34:10

leetcode 69 Sqrt(x) ---java的相关文章

LeetCode --- 69. Sqrt(x)

题目链接:Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 这道题的要求是实现int sqrt(int x),即计算x的平方根. 考虑二分,即先令l和r分别为1和x/2+1(x的平方根一定小于等于x/2+1),然后m等于(l+r)/2,不断比较m*m和x的大小. 由于m*m的时候,可能溢出,因此可以用除法代替乘法,或者采用long long类型. 时间复杂度:O(logn) 空间复杂度:O(1)

Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncat

Leetcode 69. Sqrt(x) 求整数根 in Java

69. Sqrt(x) Total Accepted: 109623 Total Submissions: 418262 Difficulty: Medium Implement int sqrt(int x). Compute and return the square root of x. public class Solution { public int mySqrt(int x) { if(x==1) return 1; //注意此题返回值int,和sqrt返回值double不同 do

Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute and return the square root of x. 分析: 解法1:牛顿迭代法(牛顿切线法) Newton's Method(牛顿切线法)是由艾萨克·牛顿在<流数法>(M

[leetcode] 69. Sqrt(x) 解题报告

Implement int sqrt(int x). Compute and return the square root of x. using only integer division for the Newton method works public int mySqrt(int x) { long r=x; while (r*r>x){ r=(r+x/r)/2; } return (int) r; }

[LeetCode] 69. Sqrt(x)_Easy tag: Binary Search

Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example

leetcode 69题 思考关于二分查找的模版

leetcode 69, Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is retu

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

69. Sqrt(x) &amp;&amp; 367. Valid Perfect Square

69. Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. Hide Tags Binary Search Math Hide Similar Problems (M) Pow(x, n) (M) Valid Perfect Square Time Limit Exceed Solution if start from 0. public class Solution { public int m