![](https://img-blog.csdnimg.cn/20201014180756919.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
剑指offer-位运算
qch60925
这个作者很懒,什么都没留下…
展开
-
剑指offer-16-计算整数次方
计算整数的次方-还是与运算移位问题 public double myPow(double x, int n) { if(n==0) return 1; if(x==0) return 0; long n1=n; double ans = 1.0; if(n1<0){ x=1/x; n1=-n1; } while(n1!=0){原创 2021-03-14 21:37:35 · 43 阅读 · 0 评论 -
剑指offer-15-二进制数字中1的个数
方法1:将n和1作与运算,n不断右移 (容易造成死循环,当为负数时) public int hammingWeight(int n) { int count=0; while(n!=0){ if((n&1)==1){ count++; } n=n>>>1; } return count; } 方法2:原创 2021-03-13 21:19:47 · 60 阅读 · 0 评论