[剑指offer] 11. 二进制中1的个数

it2025-11-09  10

题目描述

输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
利用位运算 class Solution { public: int NumberOf1(int n) { int a = 1; int nums = 0; while (a != 0) { if (n == (n | a)) nums++; a <<= 1; } return nums; } };

 

转载于:https://www.cnblogs.com/ruoh3kou/p/10049152.html

相关资源:数据结构—成绩单生成器
最新回复(0)