Skip to content

Commit 1b0de32

Browse files
committed
0069. Sqrt(x)
1 parent e888cce commit 1b0de32

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

markdown/0069. Sqrt(x).md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### [69\. Sqrt(x)](https://leetcode.com/problems/sqrtx/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Implement `int sqrt(int x)`.
7+
8+
Compute and return the square root of _x_, where _x_ is guaranteed to be a non-negative integer.
9+
10+
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: 4
16+
Output: 2
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: 8
23+
Output: 2
24+
Explanation: The square root of 8 is 2.82842..., and since
25+
  the decimal part is truncated, 2 is returned.
26+
```
27+
28+
29+
#### Solution
30+
31+
Language: **Java**
32+
33+
```java
34+
class Solution {
35+
   public int mySqrt(int x) {
36+
       int left = 0;
37+
       int right = x;
38+
       int medium = (left + right) / 2;
39+
       while (left <= right) {
40+
           if (medium == 0) {
41+
               return right;
42+
          }
43+
           int target = x / medium;
44+
           if (medium == target || medium == target + 1) {
45+
               return target;
46+
          } else if (target < medium) {
47+
               right = medium;
48+
          } else if (target > medium - 1) {
49+
               left = medium + 1;
50+
          }
51+
           medium = (left + right) / 2;
52+
      }
53+
       return 0;
54+
  }
55+
}
56+
```
57+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190720170020.png)

src/main/java/leetcode/_69_/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode._69_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println( solution.mySqrt(2147395599));
10+
System.out.println( solution.mySqrt(0));
11+
System.out.println( solution.mySqrt(1));
12+
System.out.println( solution.mySqrt(6));
13+
}
14+
}
15+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package leetcode._69_;
2+
3+
class Solution {
4+
public int mySqrt(int x) {
5+
int left = 0; // 二分法
6+
int right = x;
7+
int medium = (left + right) / 2;
8+
while (left <= right) {
9+
if (medium == 0) {
10+
return right;
11+
}
12+
int target = x / medium;
13+
if (medium == target || medium == target + 1) {
14+
return target;
15+
} else if (target < medium) {
16+
right = medium;
17+
} else if (target > medium - 1) {
18+
left = medium + 1;
19+
}
20+
medium = (left + right) / 2;
21+
}
22+
return 0;
23+
}
24+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
### [69\. Sqrt(x)](https://leetcode.com/problems/sqrtx/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Implement `int sqrt(int x)`.
7+
8+
Compute and return the square root of _x_, where _x_ is guaranteed to be a non-negative integer.
9+
10+
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
11+
12+
**Example 1:**
13+
14+
```
15+
Input: 4
16+
Output: 2
17+
```
18+
19+
**Example 2:**
20+
21+
```
22+
Input: 8
23+
Output: 2
24+
Explanation: The square root of 8 is 2.82842..., and since
25+
  the decimal part is truncated, 2 is returned.
26+
```
27+
28+
29+
#### Solution
30+
31+
Language: **Java**
32+
33+
```java
34+
class Solution {
35+
   public int mySqrt(int x) {
36+
       int left = 0;
37+
       int right = x;
38+
       int medium = (left + right) / 2;
39+
       while (left <= right) {
40+
           if (medium == 0) {
41+
               return right;
42+
          }
43+
           int target = x / medium;
44+
           if (medium == target || medium == target + 1) {
45+
               return target;
46+
          } else if (target < medium) {
47+
               right = medium;
48+
          } else if (target > medium - 1) {
49+
               left = medium + 1;
50+
          }
51+
           medium = (left + right) / 2;
52+
      }
53+
       return 0;
54+
  }
55+
}
56+
```
57+
![](https://raw.githubusercontent.com/PicGoBed/PicBed/master/20190720170020.png)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy