[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.

解题思路1

每一位分别和1进行与运算,统计结果不为0的位数。

实现代码1

//Runtime:10 ms
#include <iostream>
#include "inttypes.h"
using namespace std;

class Solution {
public:
    int hammingWeight(uint32_t n) {
        int i = 0;
        while (n)
        {
            i += n & 0x1;
            n >>= 1;
        }

        return i;
    }
};

int main()
{
    Solution s;
    cout<<s.hammingWeight(11)<<endl;
    return 0;
}

解题思路2

每次n&(n-1)可以将n里面的值为1的位数减少一位

实现代码2

//Runtime:11 ms
class Solution {
public:
    int hammingWeight(uint32_t n) {
        int i = 0;
        while (n)
        {
            n &= (n-1);
            ++i;
        }

        return i;
    }
};
时间: 2024-07-30 17:01:27

[LeetCode] Number of 1 Bits的相关文章

[LeetCode] Number of 1 Bits &amp;amp; Reverse Integer - 整数问题系列

目录:1.Number of 1 Bits  - 计算二进制1的个数 [与运算] 2.Contains Duplicate - 是否存在重复数字 [遍历]3.Reverse Integer - 翻转整数 [int边界问题]4.Excel Sheet Column Number - Excel字符串转整数 [简单]5.Power of Two & Happy Number - 计算各个位数字 [%10 /10] 一.Number of 1 Bits  题目概述:Write a function t

[LeetCode]191.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 r

[LeetCode] Binary Number with Alternating Bits 有交替位的二进制数

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1: Input: 5 Output: True Explanation: The binary representation of 5 is: 101 Example 2: Input: 7 Output: False Ex

【LeetCode从零单排】No 191.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 r

[LeetCode] Number of Distinct Islands 不同岛屿的个数

Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water. Count the number of distinct island

[LeetCode] Number of Longest Increasing Subsequence 最长递增序列的个数

Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: Input: [1,3,5,4,7] Output: 2 Explanation: The two longest increasing subsequence are [1, 3, 4, 7] and [1, 3, 5, 7]. Example 2: Input: [2,2,2,2,2] Outpu

[LeetCode] Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by

[笔试题目] 美团2015年9月后端开发工程师笔试题

由于题目是我通过草稿回顾,可能表述不清,但是内容大致一样.希望该博客内容对你有所帮助,题目所有权归美团公司所有,我只是想分享给大家学习,还望贵公司海涵~ 面试职位 应聘职位:后端开发工程师 岗位描述: 岗位要求: 面试时间:2015年9月19日 面试题型:90分钟 16单选+4多选+2编程 单选题 第1题 从A->B路程中有段扶梯,某人途中需要绑鞋带,问那种情况更快? A.路上绑鞋带时间快 B.扶梯上绑鞋带时间快 C.时间一样 D.扶梯路程和绑鞋带时间左右 第2题 X86平台上,int型变量内存

LeetCode All in One 题目讲解汇总(持续更新中...)

终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 如果各位看官们,大神们发现了任何错误,或是代码无法通过OJ,或是有更好的解法,或是有任何疑问,意见和建议的话,请一定要在对应的帖子下面评论区留言告知博主啊,多谢多谢,祝大家刷得愉快,刷得精彩,刷出美好未来- 博主制作了一款iOS的应用"Leetcode Meet Me",里面有Leetcode上所有的题目,并且贴上了博主的解法,随时随地都能