File tree Expand file tree Collapse file tree 4 files changed +153
-0
lines changed
src/main/java/leetcode/_69_ Expand file tree Collapse file tree 4 files changed +153
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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
+
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 )
You can’t perform that action at this time.
0 commit comments