【LeetCode】Sqrt(x) (2 solutions)

Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

解法一:牛顿迭代法

求n的平方根,即求f(x)=x2-n的零点

设初始值为x0,注,不要设为0,以免出现除数为0,见后。

则过(x0,f(x0))点的切线为g(x)=f(x0)+f‘(x0)*(x-x0)

g(x)与x轴的交点为x1=x0-f(x0)/f‘(x0)

递推关系为xn+1=xn-f(xn)/f‘(xn)

当收敛时即为解。

class Solution {
public:
    int sqrt(int x) {
        double x0 = 1;
        double xn = (x0+x/x0)/2;
        while(abs(x0-xn) > 1e-5)
        {
            x0 = xn;
            xn = (x0+x/x0)/2;
        }
        return x0;
    }
};

解法二:二分法

注意返回为int,结果会取整。

class Solution {
public:
    int sqrt(int x) {
        long long low = 0;
        long long high = x;
        long long mid;
        while(low <= high)
        {
            mid = (low+high)/2;
            long long result = mid*mid;
            if(result == x)
                return mid;
            else if(result > x)
                high = mid-1;
            else
                low = mid+1;
        }
        return high;
    }
};

时间: 2024-10-09 16:22:23

【LeetCode】Sqrt(x) (2 solutions)的相关文章

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】Reverse Integer (2 solutions)

Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have alread

【leetcode】Sqrt(x)

Implement int sqrt(int x). Compute and return the square root of x. 1 class Solution { 2 public: 3 int mySqrt(int x) { 4 unsigned long long begin=0; 5 unsigned long long end=(x+1)/2; 6 unsigned long long mid; 7 unsigned long long tmp; 8 9 while(begin

【LeetCode】Same Tree (2 solutions)

Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解法一:递归 /** * Definition for binary tree * struct TreeNod

【LeetCode】Plus One (2 solutions)

Plus One Given a non-negative number represented as an array of digits, plus one to the number. The digits are stored such that the most significant digit is at the head of the list. 从个位数字往前扫,只要一个数字不为9,即可加1返回. 如果直到最高位仍为9,则说明结果为100…0 解法一: 可能会使用额外空间与复制

【LeetCode】Generate Parentheses (2 solutions)

Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

【LeetCode】Climbing Stairs (2 solutions)

Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 这是最简单的DP了,递推公式为f(n)=f(n-1)+f(n-2) 因此建立vector存放中间结果即可. class Solution

【LeetCode】Symmetric Tree (2 solutions)

Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note:Bonus points if you could sol

【LeetCode】Next Permutation (2 solutions)

Next Permutation Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending orde