Skip to content

Commit a62b5b9

Browse files
refactor 69
1 parent b269670 commit a62b5b9

File tree

1 file changed

+10
-11
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+10
-11
lines changed
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 69. Sqrt(x)
45
* Implement int sqrt(int x).
5-
6-
Compute and return the square root of x.
7-
8-
* Created by fishercoder on 1/25/17.
6+
* Compute and return the square root of x.
97
*/
8+
109
public class _69 {
1110
public int mySqrt(int x) {
12-
long i = 0;
13-
long j = x / 2 + 1;
14-
while (i <= j) {
15-
long mid = (i + j) / 2;
11+
long left = 0;
12+
long right = x / 2 + 1;
13+
while (left <= right) {
14+
long mid = left + (right - left) / 2;
1615
long result = mid * mid;
1716
if (result == (long) x) {
1817
return (int) mid;
1918
} else if (result > x) {
20-
j = mid - 1;
19+
right = mid - 1;
2120
} else {
22-
i = mid + 1;
21+
left = mid + 1;
2322
}
2423
}
25-
return (int) j;
24+
return (int) right;
2625
}
2726
}

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