69. Sqrt(x)
ๆณจๆ range ๆฏ 1 ~ x, ่ฆๆพๅนณๆนๆ น
left ~ right: 1~x
finally, return right
A little trick is using i <= x / i for comparison,
instead of i * i <= x, to avoid exceeding integer upper limit.
Binary Search Solution:
Time complexity = O(lg(x)) = O(32)= O(1) (log(2^32) = 32)
space: O(1)
public int mySqrt(int x) {
int left = 1; //1~x
int right = x;
while (left <= right) {
int mid = left + (right - left) / 2; // (left + right) >>> 1
if (mid == x / mid) { // 4 8/4=2
return mid;
} else if (mid < x / mid) {
left = mid + 1;
} else {
right = mid - 1; //3
}
}
return right; // in the end, right must < left, so ans is right
}
ๅฆไธ็จฎๆ่ทฏ
https://leetcode-cn.com/problems/sqrtx/solution/er-fen-cha-zhao-niu-dun-fa-python-dai-ma-by-liweiw/
class Solution {
public int mySqrt(int x) {
int left = 0;
int right = x;
while (left < right) {
int mid = (left + right + 1) >>> 1; //ๅๅณไธญไฝๆธ
// we want to select mid*mid <= x
// so use mid > x/mid in if
if (mid > x/mid) {
right = mid - 1;
} else {
left = mid;
}
}
return left;
}
}
new style
use x/mid, ไฝ่ฆๆณจๆ edge case (0,1)
```java
class Solution {
public int mySqrt(int x) {
if (x == 0 || x == 1) { // avoid edge case, divide by 0, and 1...
return x;
}
int left = 0;
int right = x;
int bound = 0;
while (left <= right) {
int mid = left + (right - left)/2;
int current = x/mid;
if (mid == current) {
return (int)mid;
} else if (mid < current) { // ๅจ็ฏๅๅ
ง, ๅๅผ
bound = Math.max(bound, mid);
left = mid + 1;
} else {
right = mid - 1;
}
}
return bound;
}
}
```
use ไนๆณ, ไฝ่ฆ long, ไธ็ถๆๆบขไฝ
class Solution {
public int mySqrt(int x) {
long left = 0;
long right = x;
long bound = 0;
while (left <= right) {
long mid = left + (right - left)/2;
long current = mid*mid;
if (current == x) {
return (int)mid;
} else if (current < x) { // ๅจ็ฏๅๅ
ง, ๅ็ตๆ
bound = Math.max(bound, mid);
left = mid + 1;
} else {
right = mid - 1;
}
}
return (int)bound;
}
}
Last updated