File tree Expand file tree Collapse file tree 1 file changed +10
-11
lines changed
src/main/java/com/fishercoder/solutions Expand file tree Collapse file tree 1 file changed +10
-11
lines changed Original file line number Diff line number Diff line change 11package 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+
109public 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}
You can’t perform that action at this time.
0 commit comments