【一天一道LeetCode】#69. Sqrt(x)

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Implement int sqrt(int x).

Compute and return the square root of x.

(二)解题

实现sqrt(x),找到一个数,它的平方等于小于x的最接近x的数。

class Solution {
public:
    int mySqrt(int x) {
        int i = 0 ;
        int j = x
        while(i<j)
        {
            long mid = (i+j)/2 +1;//二分查找取中间值
            if(mid*mid>x) j = mid-1;
            else if(mid*mid<x) i = mid;
            else if(mid*mid==x) return mid;//找到
        }
        return i;//没找到就返回i
    }
};
时间: 2024-10-09 02:06:11

【一天一道LeetCode】#69. Sqrt(x)的相关文章

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) 解题报告【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) 求整数根 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) ---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 )

[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】#342. Power of Four

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. F

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