// var myPow = function (x, n) {
// let res = 1;
// for (let i = Math.abs(n); i !== 0; i = Math.floor(i / 2)) {
// if (i % 2 !== 0) {
// res *= x;
// }
// x *= x;
// }
// return n < 0 ? 1 / res : res;
// };
var myPow = function (x, n) {
if (n === 0) return 1;
if (n === 1) return x;
const absN = Math.abs(n);
const isNegative = n !== absN;
let res = absN % 2 ? x * myPow(x, absN - 1) : myPow(x * x, absN / 2)
return isNegative ? 1 / res : res;
};
// console.log(myPow(2, 2))
let mySqrt = function(x) {
let left = 0;
let right = x;
let ans = -1;
while(left <= right) {
let mid = Math.round((left + right) / 2);
let temp = mid * mid;
console.log(temp, left, right)
if(temp <= x) {
ans = mid;
left = mid + 1;
} else if(temp > x) {
right = mid - 1;
}
}
console.log(ans);
}
// mySqrt(4)
let search = function(nums, target) {
let left = 0;
let right = nums.length - 1;
while(left <= right) {
let mid = Math.round((left + right) / 2);
if(nums[mid] < target) {
left = mid + 1;
} else if(nums[mid] > target) {
right = mid - 1;
} else {
return mid;
}
}
}
// console.log(search([1, 3, 4, 5, 6, 8], 4))
console