力扣刷题笔记,结合大神们的题解x小白
剑指 Offer II 072. 求平方根
- 使用sqrt()函数
- 袖珍计算器:exp函数和ln函数代替平方根,根号x = e的(二分之一✖️lnx)次方;(计算机无法存储浮点数的精确值,会存在误差,需要判断ans和ans+1)
class Solution {
public:
int mySqrt(int x) {
int ans = exp(0.5*log(x));
if (pow(ans+1,2)<=x){
return ans+1;
}
return ans;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/jJ0w9p/solution/qiu-ping-fang-gen-by-leetcode-solution-ybnw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 二分查找
class Solution {
public:
int mySqrt(int x) {
int l =0, r=x,mid,ans;
while(l<=r){
//mid = l+(r-l)/2;
mid = (l+r)/2;
if(pow(mid,2)<=x){
ans = mid;
l = mid+1;
}
else{
r=mid-1;
}
}
return ans;
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/jJ0w9p/solution/qiu-ping-fang-gen-by-leetcode-solution-ybnw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 牛顿迭代(即快速求函数零点的方法)
转化为y = f(x) = x^2 - C
的零点。借助泰勒级数,从初值开始向零点逼近,任取一个x0作为初数值,每步迭代中,找到点(xi,f(xi)),过该点做斜率为该点导数f’(xi)的直线,交于(xi+1,0)。xi+1相较于xi离零点更近,多次迭代得到交点。
选x0=C作为初值,每次迭代得到点(xi,xi^2-C),k=2xi,直线与x轴交点xi+1 = 0.5*(xi+C/xi)
class Solution {
public:
int mySqrt(int x) {
if (x == 0) {
return 0;
}
double C = x, x0 = x;
while (true) {
double xi = 0.5 * (x0 + C / x0);
if (fabs(x0 - xi) < 1e-7) {
break;
}
x0 = xi;
}
return int(x0);
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/jJ0w9p/solution/qiu-ping-fang-gen-by-leetcode-solution-ybnw/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
评论区bookyue大神优化成整数:
class Solution {
public int mySqrt(int x) {
long s = x;
while (s * s > x)
s = (s + x / s) / 2;
return (int) s;
}
}
剑指 Offer 16. 数值的整数次方
- 快速幂+递归
官方代码
class Solution {
public:
double quickMul(double x, long long N) {
if (N == 0) {
return 1.0;
}
double y = quickMul(x, N / 2);
return N % 2 == 0 ? y * y : y * y * x;
}
double myPow(double x, int n) {
long long N = n;
return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/solution/shu-zhi-de-zheng-shu-ci-fang-by-leetcode-yoqr/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
我的代码
class Solution {
public:
double myPow(double x, long long n) {
//n从int改为long long,否则溢出
if(n==0){
return 1.0;
}
if(n<0){
return 1.0/myPow(x,-n);
}//负数单独判断转为正数
double ans = myPow(x,n/2);
//ans需要为double,否则小数运算不正确
if(n%2==0){
return 1.0*ans*ans;
}
else{
return 1.0*ans*ans*x;//向下取整
}
}
};
- 快速幂+迭代
class Solution {
public:
double quickMul(double x, long long N) {
double ans = 1.0;
// 贡献的初始值为 x
double x_contribute = x;
// 在对 N 进行二进制拆分的同时计算答案
while (N > 0) {
if (N % 2 == 1) {
// 如果 N 二进制表示的最低位为 1,那么需要计入贡献
ans *= x_contribute;
}
// 将贡献不断地平方
x_contribute *= x_contribute;
// 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可
N /= 2;
}
return ans;
}
double myPow(double x, int n) {
long long N = n;
return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
}
};
作者:LeetCode-Solution
链接:https://leetcode.cn/problems/shu-zhi-de-zheng-shu-ci-fang-lcof/solution/shu-zhi-de-zheng-shu-ci-fang-by-leetcode-yoqr/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
my code
class Solution {
public:
double myPow(double x, long long n) {
if(n==0){
return 1.0;
}
if(n<0){
return 1.0/myPow(x,-n);
}
double ans=1.0;
while(n){
if(n&1==1){
ans = ans*x;
}
x=x*x;
n=n>>1;
}
return ans;
}
};
231 2的幂
- 暴力
class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=0){
return false;
}
if(n==1){
return true;
}
long long ans=1;
while(1){
ans=ans*2;
if(ans==n){
return true;
}
else if(ans>n){
return false;
}
}
return false;
}
};
- 二进制
n&(n-1)==0
class Solution {
public:
bool isPowerOfTwo(int n) {
if((n>0)&&(n&(n-1))==0){
return true;
}
return false;
}
};
n&(-n)==n
由于负数是按照补码规则在计算机中存储的,-n的二进制表示为n的二进制取反+1
return n > 0 && (n & -n) == n;
面试题 16.01. 交换数字
利用位运算
class Solution {
public int[] swapNumbers(int[] numbers) {
numbers[0] = numbers[0] ^ numbers[1];
numbers[1] = numbers[0] ^ numbers[1];
numbers[0] = numbers[0] ^ numbers[1];
return numbers;
}
}
作者:yugaowei
链接:https://leetcode.cn/problems/swap-numbers-lcci/solution/wei-yun-suan-yi-ji-ta-bei-hou-de-yuan-li-83ce/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。