Total Accepted: 75767 Total Submissions: 314003 Difficulty: Medium
Implement int sqrt(int x)
.
Compute and return the square root of x.
class Solution { public: int mySqrt(int x) { if(x<0){ return INT_MIN; } if(x==0){ return 0; } long long int low = 1; long long int high= (long long int)x+1; while(low<high){ long long int mid = low + (high-low)/2; if(mid > x/mid){ high = mid; }else if(mid == x/mid){ return mid; }else{ low = mid+1; } } return low-1; } };
Next challenges: (M) Kth Smallest Element in a BST (M) Number of Digit One (E) Closest Binary Search Tree Value
时间: 2024-08-23 18:48:28