LeetCode:Sqrt(x) 解题报告

Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

SOLUTION 1:

参见:二分法总结,以及模板:http://t.cn/RZGkPQc

 1 public class Solution {
 2     public int sqrt(int x) {
 3         if (x == 1 || x == 0) {
 4             return x;
 5         }
 6
 7         int left = 1;
 8         int right = x;
 9
10         while (left < right - 1) {
11             int mid = left + (right - left) / 2;
12             int quo = x / mid;
13
14             if (quo == mid) {
15                 return quo;
16             // mid is too big
17             } else if (quo < mid) {
18                 right = mid;
19             } else {
20                 left = mid;
21             }
22         }
23
24         return left;
25     }
26 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/Sqrt.java

时间: 2024-12-28 00:25:16

LeetCode:Sqrt(x) 解题报告的相关文章

LeetCode: Combination Sum 解题报告

Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Question Solution Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The

[LeetCode]LRU Cache, 解题报告

题目 Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.

【LeetCode】Subsets 解题报告

[题目] Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,

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 ZigZag Conversion 解题报告

对输入字符串,做蛇形变化,然后按行输出. https://oj.leetcode.com/problems/zigzag-conversion/ 例如:The string "PAYPALISHIRING"  的蛇形变化如下: P        A           H        N A   P   L    S     I     I   G Y         I            R 最后要求输出的就是:"PAHNAPLSIIGYIR" Write

LeetCode: Gas Station 解题报告

Gas Station There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journ

Leetcode:Interleaving String 解题报告

Interleaving StringGiven s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false.

Leetcode:Scramble String 解题报告

Scramble String Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string,