leetcode_191题——Number of 1 Bits(外运算)

Number of 1 Bits

Total Accepted: 25965 Total Submissions: 70075My Submissions

Question Solution

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.

Hide Tags

Bit Manipulation

Have you met this question in a real interview?

Yes

No

Discuss

这道题考虑按位与运算和位的左移右移就可以了

#include<iostream>
#include<bitset>
typedef unsigned int uint32_t;

using namespace std;

int hammingWeight(uint32_t n)
{
	unsigned int temp=1;
	int result=0;
	for(int i=0;i<32;i++)
	{
		int temp2=temp<<i;
		if((n&temp2)!=0)
			result+=1;
	}
	return result;
}

int main()
{
	cout<<hammingWeight(11)<<endl;

}

  

时间: 2024-11-03 21:12:33

leetcode_191题——Number of 1 Bits(外运算)的相关文章

leetcode_171题——Number of 1 Bits(位运算)

Number of 1 Bits Total Accepted: 38248 Total Submissions: 101730My Submissions Question Solution 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 int

LeetCode算法题-Number of 1 Bits(Java实现)

这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1"位数.例如: 输入:11 输出:3 说明:整数11具有二进制表示00000000000000000000000000001011 输入:128 输出:1 说明:整数128具有二进制表示00000000000000000000000010000000 本次解题使用的开发工具是eclipse,jdk使

Leetcode 191 Number of 1 Bits 位运算

统计一个值的二进制1的个数,用与(&)和向左移位(<<) 编程之美中的2.1节有详细解答. 解法一: 1 class Solution { 2 public: 3 int hammingWeight(uint32_t n) { 4 int ans = 0; 5 for(;n;n = n>>1){ 6 ans += n&1; 7 } 8 return ans; 9 } 10 }; 解法二: 1 class Solution { 2 public: 3 int hamm

【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

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

LeetCode191 Number of 1 Bits. LeetCode231 Power of Two. LeetCode342 Power of Four

位运算相关 三道题 231. Power of Two Given an integer, write a function to determine if it is a power of two. (Easy) 分析: 数字相关题有的可以考虑用位运算,例如&可以作为筛选器. 比如n & (n - 1) 可以将n的最低位1删除,所以判断n是否为2的幂,即判断(n & (n - 1) == 0) 代码: 1 class Solution { 2 public: 3 bool isP

【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

LeetCode 191:number of one 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 shoul

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 retu