剑指offer-二进制中1的个数

题目地址:https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8?tpId=13&&tqId=11164&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

n=n&(n-1);

核心代码

 1 class Solution {
 2 public:
 3      int  NumberOf1(int n) {
 4          int res=0;
 5          while(n!=0){
 6              res++;
 7              n=n&(n-1);
 8          }
 9          return res;
10      }
11 };

 

posted @ 2020-09-17 12:32  LifeRunningError  Views(112)  Comments(0Edit  收藏  举报