【LeetCode从零单刷】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.

解答:

其实一开始看题我就懵比了。。。11变00000000000000000000000000001011?后来想清楚11是十进制的“十一”。。。

题意是统计一个数字二进制化之后的,1的个数。最直接的方法就是不断 mod 2 判断是不是 1 之类的。听说这样会超时……想想,二进制,是不是可以引入位运算(一般来说,位运算较于加减乘除之类的计算要快速很多,能用就多用)。

可以将原数不断右移,然后与1位与运算(^),判断最后一位是不是1,是就count++。知道原数变0之后停止。

但是有个问题,假如问题扩展到负数,怎么办?不断右移之后,符号位不变一直是1还不就是无限循环了么?这时候可以引入汉明重量,基于一种事实即 X 与 X-1 位与得到的最低位永远是 0。例如:

Expression Value
X 0 1 0 0 0 1 0 0 0 1 0 0 0 0
X-1 0 1 0 0 0 1 0 0 0 0 1 1 1 1
X & (X-1) 0 1 0 0 0 1 0 0 0 0 0 0 0 0

减 1 操作将最右边的符号从 0 变到 1,从 1 变到 0。这样操作之后的直接结果是,原数少了个1位。而且对于负数也同样有循环终点。

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int count = 0;
        while(n)
        {
            count++;
            n = n&(n-1);
        }
        return count;
    }
};

觉得我讲解的不够清楚的,可以看看:点击打开链接

另外留一个小问题:如何判断一个数是不是2的整数次幂?(提示:看到2,想到位运算!利用汉明重量)

时间: 2024-08-28 09:16:31

【LeetCode从零单刷】Number of 1 Bits的相关文章

【LeetCode从零单刷】Binary Tree Inorder Traversal

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2].  Note: Recursive solution is trivial, could you do it iteratively? 解答: 其实思路有点类似以前的一篇文章:[LeetCode从零单刷]Binary

【LeetCode从零单刷】Excel Sheet Column Number

题目: Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 解答: 怎么说,类似26进制数一样.乍看之下挺简单.于是写下: class Solution { public: int titleToNumber(stri

【LeetCode从零单刷】Ugly Number

题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. Note that 1 i

【LeetCode从零单刷】Find the Duplicate Number

题目: Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Note: You must not modif

【LeetCode从零单刷】Single Number II

题目: Given an array of integers, every element appears three times except for one. Find that single one. 解答: 如果是挑出非偶数次数的元素,可以利用每个元素依次异或的方法.但是非奇数次数(3,5,7,9--)的元素? 如果要快,可以hash_map存储.然后遍历一遍hash_map找到Single Number. 换种思路,按位找不同的Single Number对应的位.细节上: bit =

【LeetCode从零单刷】Maximum Depth of Binary Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given a binary tree, find its maximum depth.  The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 解答: 树的深度优先遍历,求得树高度即可.然后需要用到递归的思想,假设子树的高

【LeetCode从零单刷】Same Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷LeetCode! 题目: Given two binary trees, write a function to check if they are equal or not.  Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 解答: 验证两棵树是否相等.使

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

【LeetCode从零单刷】N-Queens II

题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total number of distinct solutions. 解答: 思路很简单,就是暴力求解的N皇后问题计数.过程如下: 如果第 i 行的第 j 列放着皇后,然后放第 (i+1) 行的皇后使其不矛盾,然后第 (i+2) 行--: 如果每一列都不可行,那我们就回溯一行,然后继续第 1 步直至成功(转3)