lintcode586- Sqrt(x) II- medium

Implement double sqrt(double x) and x >= 0.

Compute and return the square root of x.

Notice

You do not care about the accuracy of the result, we will help you to output results.

Example

Given n = 2 return 1.41421356

二分模板法,只不过从start + 1 < end 变为start + eps < end。 eps是比你要求的精度小一点的一个数,比如1e-12(10的-12次方)。

另一个是牛顿迭代法。数学上的, 迭代去算,精度不够就进入while就可以了。

九章解析:

// 二分浮点数 和二分整数不同
// 一般都有一个精度的要求 譬如这题就是要求小数点后八位
// 也就是只要我们二分的结果达到了这个精度的要求就可以
// 所以 需要让 right 和 left 小于一个我们事先设定好的精度值 eps
// 一般eps的设定1e-8,因为这题的要求是到1e-8,所以我把精度调到了1e-12
// 最后 选择 left 或 right 作为一个结果即可

public class Solution {
    /*
     * @param x: a double
     * @return: the square root of x
     */
    public double sqrt(double x) {
        // write your code here
        double eps = 1e-12;
        double start = 0;
        double end = x <= 1.0 ? 1.0 : x;

        while (start + eps < end){
            double mid = start + (end - start) / 2;
            if (mid * mid < x){
                start = mid;
            } else {
                end = mid;
            }
        }

        return start;
    }
}
时间: 2024-10-10 20:40:03

lintcode586- Sqrt(x) II- medium的相关文章

[leetcode] 040. Combination Sum II (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 040. Combination Sum II (Medium) 链接: 题目:https://leetcode.com/problems/combination-sum-ii/ 代码(github):https://github.com/illuz/leetcode 题意: 跟 039 一样(给出一些正整数集合,

[array] leetcode - 40. Combination Sum II - Medium

leetcode - 40. Combination Sum II - Medium descrition Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combinat

leetCode 213. House Robber II | Medium | Dynamic Programming

213. House Robber II Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are a

leetcode496 - Next Greater Element I - easy &amp;&amp; leetcode503 - Next Greater Element II - medium

496. Next Greater Element IYou are given two arrays (without duplicates) nums1 and nums2 where nums1's elements are subset of nums2. Find all the next greater numbers for nums1's elements in the corresponding places of nums2.The Next Greater Number o

122. Best Time to Buy and Sell Stock II [medium] (Python)

题目链接 https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题目原文 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions

lintcode143 - Sort Colors II - medium

Given an array of n objects with k different colors (numbered from 1 to k), sort them so that objects of the same color are adjacent, with the colors in the order 1, 2, ... k.ExampleGiven colors=[3, 2, 2, 1, 4], k=4, your code should sort colors in-p

leetcode253 - Meeting Rooms II - medium

Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si < ei), find the minimum number of conference rooms required.Example 1:Input: [[0, 30],[5, 10],[15, 20]]Output: 2Example 2:Input: [[7,10],[2,4]]Output

92. Reverse Linked List II - Medium

Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Example: Input: 1->2->3->4->5->NULL, m = 2, n = 4 Output: 1->4->3->2->5->NULL 只反转从m到n的部分链表 M1: iterative 首先找到prev的位置(开始反转位置的前一个节

445. Add Two Numbers II - Medium

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers

364. Nested List Weight Sum II - Medium

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Different from the previous question where weight