【LeetCode】191. Number of 1 Bits
LeetCode of number Bits
2023-09-11 14:20:27 时间
Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).
For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011
, so the function should return 3.
Credits:
Special thanks to @ts for adding this problem and creating all test cases.
思路:n&(n-1)消除一个最低位的1
class Solution { public: int hammingWeight(uint32_t n) { int count = 0; while(n) { n &= (n-1); count ++; } return count; } };
相关文章
- Leetcode 之Length of Last Word(37)
- Java实现 LeetCode 838 推多米诺(暴力模拟)
- Java实现 LeetCode 714 买卖股票的最佳时机含手续费(动态规划 || 迭代法)
- Java实现 LeetCode 673 最长递增子序列的个数(递推)
- Java实现 LeetCode 372 超级次方
- Java实现 LeetCode 372 超级次方
- Java实现 LeetCode 371 两整数之和
- Java实现 LeetCode 331 验证二叉树的前序序列化
- Java实现 LeetCode 54 螺旋矩阵
- 【LeetCode算法-58/66】Length of Last Word/Plus One
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
- [LeetCode] Intersection of Two Linked Lists
- LeetCode(60): 第k个排列
- LeetCode-792. 匹配子序列的单词数【字典树,哈希表,二分查找】
- leetcode 题解 || Remove Nth Node From End of List 问题
- [LeetCode] 104. Maximum Depth of Binary Tree ☆(二叉树的最大深度)
- Leetcode:signal_number_ii
- leetcode 217 Contains Duplicate 数组中是否有反复的数字
- leetcode 458. Poor Pigs
- leetcode 326. Power of Three
- leetcode 387. First Unique Character in a String
- leetcode 104. Maximum Depth of Binary Tree
- 【Leetcode刷题Python】222. 完全二叉树的节点个数
- 【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
- LeetCode 2.两数相加
- 【LeetCode】200. 岛屿数量