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

思路

每次左移一位,进行&运算。

代码

/*------------------------------------------------------
*   日期:2014-04-12
*   作者:SJF0115
*   题目: 191.Number of 1 Bits
*   网址:https://leetcode.com/problems/number-of-1-bits/
*   结果:AC
*   来源:LeetCode
*   博客:
--------------------------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    int hammingWeight(uint32_t n) {
        if(n == 0){
            return 0;
        }//if
        if(n == 1){
            return 1;
        }//if
        int count = 0;
        for(int i = 0;i < 32;++i){
            if((n&(1<<i)) > 0){
                ++count;
            }//if
        }//for
        return count;
    }
};

int main(){
    Solution solution;
    uint32_t num = 1;
    cout<<""<<solution.hammingWeight(num)<<endl;
    return 0;
}

思路二

每执行一次x = x&(x-1),会将x用二进制表示时最右边的一个1变为0,因为x-1将会将该位(x用二进制表示时最右边的一个1)变为0。

代码二

/*------------------------------------------------------
*   日期:2014-04-12
*   作者:SJF0115
*   题目: 191.Number of 1 Bits
*   网址:https://leetcode.com/problems/number-of-1-bits/
*   结果:AC
*   来源:LeetCode
*   博客:
--------------------------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;

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

int main(){
    Solution solution;
    uint32_t num = 11;
    cout<<""<<solution.hammingWeight(num)<<endl;
    return 0;
}
时间: 2024-11-17 19:45:07

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

[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 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] 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 retu

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

[LeetCode] Ugly Number II

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. Note that 1 is typically treated as an

[LeetCode] Palindrome Number &amp;amp; Valid Palindrome - 回文系列问题

题目概述: Determine whether an integer is a palindrome. Do this without extra space. 题目分析: 判断数字是否是回文 例如121.656.3443 方法有很多,正着看和到着看两数相同:当然负数显然不是回文 我的方法: 第一种方法: 由于没有没有看到前面的without extra space.采用的方法是把数字转换为字符串,依次比较最前和最后两个字符是否相同,直到遍历完毕. /** * 判断一个数字是否是回文数字 Pal

[LeetCode] Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the

[LeetCode] Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解题思路 考虑全部用二进制表示,如果我们把 第 ith 个位置上所有数字的