lintcode141- Sqrt(x)- easy

Implement int sqrt(int x).

Compute and return the square root of x.

Example

sqrt(3) = 1

sqrt(4) = 2

sqrt(5) = 2

sqrt(10) = 3

Challenge

O(log(x))

halfhalf二分法。思想一样,就是取mid后判断的是mid*mid和x的关系。注意用long类型避免乘法溢出。

public class Solution {
    /*
     * @param x: An integer
     * @return: The sqrt of x
     */
    public int sqrt(int x) {
        // write your code here
        if (x < 0){
            throw new IllegalArgumentException();
        }

        long start = 0;
        long end = x;

        while (start + 1 < end){
            long mid = start + (end - start) / 2;
            long mult = mid * mid;
            if (x < mult){
                end = mid;
            } else if (x == mult){
                return (int)mid;
            } else {
                start = mid;
            }
        }

        if (end * end == x){
            return (int)end;
        }

        return (int)start;
    }
}
时间: 2024-10-03 13:09:49

lintcode141- Sqrt(x)- easy的相关文章

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题解——算法思想之二分查找

1. 求开方 2. 大于给定元素的最小元素 3. 有序数组的 Single Element 4. 第一个错误的版本 5. 旋转数组的最小数字 6. 查找区间 正常实现 Input : [1,2,3,4,5] key : 3 return the index : 2 public int binarySearch(int[] nums, int key) { int l = 0, h = nums.length - 1; while (l <= h) { int m = l + (h - l) /

二分查找---求开方

求开方 69. Sqrt(x) (Easy) Input: 4 Output: 2 Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part 题目描述: ??给定一个整数,求该整数的开方数. 思路分析: ??一个数 x 的开方 sqrt 一定在 0 ~ x 之间,并且满足 sqrt == x / sqrt.

20191230-20200102总结(1)

LeetCode66. Plus One - Easy LeetCode67. Add Binary - Easy LeetCode69. Sqrt(x) - Easy 二分找 LeetCode70. Climbing Stairs - Easy dp LeetCode71. Simplify Path - Medium Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert

LeetCode 69 _ Sqrt(x) 求平方根 (Easy)

Description: 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

HDU4565 So Easy!

1 /* 2 HDU4565 So Easy! 3 http://acm.hdu.edu.cn/showproblem.php?pid=4565 4 数论 快速幂 矩阵快速幂 5 题意:求[(a+sqrt(b))^n ]%m [ ]代表向上取证 6 联想到斐波那契数列,所以第一感觉是矩阵快速幂 7 后来发现根本不用这么麻烦,我们认为a+b*sqrt(c)中的a为实部 8 b为虚部,就能直接用二元乘法模拟矩阵快速幂了,因此这两种方法 9 是等价的.于是乎,我们就解决了精度问题. 10 然而即使我们

Poj 2826 An Easy Problem?!

地址:http://poj.org/problem?id=2826 题目: An Easy Problem?! Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13016   Accepted: 2003 Description It's raining outside. Farmer Johnson's bull Ben wants some rain to water his flowers. Ben nails tw

hdu4565---So Easy!(矩阵)

Problem Description A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn. You, a top coder, say: So easy! Input There are several test cases, each test case in one lin

hdu 5572 An Easy Physics Problem 圆+直线

An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1430    Accepted Submission(s): 270 Problem Description On an infinite smooth table, there's a big round fixed cylinder an

HDU 2601 An easy problem(暴力枚举/质因子分解)

An easy problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7963    Accepted Submission(s): 1920 Problem Description When Teddy was a child , he was always thinking about some simple math p