面试150 位1的个数 位运算 2026/9/30 17:57:34 拓冰建站 浏览量 Problem: 191. 位1的个数 文章目录 思路复杂度Code 思路 👨🏫 参考 复杂度 Code public class Solution {// you need to treat n as an unsigned valuepublic int hammingWeight(int n){int res = 0;while (n != 0){res += 1;n &= n - 1;// 把最后一个出现的 1 改为 0,和 lowbit 有异曲同工之妙}return res;} }
Problem: 191. 位1的个数 文章目录 思路复杂度Code 思路 👨🏫 参考 复杂度 Code public class Solution {// you need to treat n as an unsigned valuepublic int hammingWeight(int n){int res = 0;while (n != 0){res += 1;n &= n - 1;// 把最后一个出现的 1 改为 0,和 lowbit 有异曲同工之妙}return res;} }