Flashcards
Sqrt(x)
Flashcards do conjunto (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;
}
}
Informações
- Descrição
- FlashCards sobre Sqrt(x), criado por Suhas S em 23-03-2018.
- Criado por
- Suhas S
- Visualizações
- 0
- Avaliação