File tree 1 file changed +10
-11
lines changed
src/main/java/com/fishercoder/solutions
1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change 1
1
package com .fishercoder .solutions ;
2
2
3
3
/**
4
+ * 69. Sqrt(x)
4
5
* 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.
9
7
*/
8
+
10
9
public class _69 {
11
10
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 ;
16
15
long result = mid * mid ;
17
16
if (result == (long ) x ) {
18
17
return (int ) mid ;
19
18
} else if (result > x ) {
20
- j = mid - 1 ;
19
+ right = mid - 1 ;
21
20
} else {
22
- i = mid + 1 ;
21
+ left = mid + 1 ;
23
22
}
24
23
}
25
- return (int ) j ;
24
+ return (int ) right ;
26
25
}
27
26
}
You can’t perform that action at this time.
0 commit comments