Karteikarten
Sqrt(x)
Karten in diesem Stapel (1)
- 1In while, If mid < sqrt(x), left = mid + 1 executed, right pointer is not moving, and right is the answer. If while, If mid > sqrt(x), right = mid - 1 executed, right pointer shifts left 1, closest to sqrt(x), right is also the answer.class Solution {
public int mySqrt(int x) {
if( x == 0 )return 0;
int left = 1; int right = x;
while(left<= right){
int mid = left + (right - left)/2;
if(mid == x/mid) return mid;
if(mid < x/mid) left = mid + 1;
else right = mid - 1;
}
return right;
}
}
Informationen
- Beschreibung
- Karteikarten am Sqrt(x), erstellt von Suhas S am 23/03/2018.
- Erstellt von
- Suhas S
- Aufrufe
- 0
- Bewertung