【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的数量。

对HammingWeight进行相关拓展。

C++:

 1 class Solution {
 2 public:
 3     int hammingWeight(uint32_t n) {
 4         int cnt=0;
 5         while(n!=0)
 6         {
 7             cnt+=n&1; //not cnt=cnt+n&1  because + is prior than &, and & than +=
 8             n=n>>1;
 9         }
10         return cnt;
11     }
12 };

Python:

1 class Solution:
2     # @param n, an integer
3     # @return an integer
4     def hammingWeight(self, n):
5         cnt=0;
6         while n !=0:
7             cnt+=n&1
8             n=n>>1
9         return cnt
时间: 2024-10-25 16:38:22

【leetcode】Number of 1 Bits的相关文章

【leetcode】Number of 1 Bits (easy)

做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { int num = 0; for(;n; n &=(n-1), num++); return num; } };

【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 surrounde

【LeetCode】Number of Islands

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

【LeetCode】位运算 bit manipulation(共32题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [78]Subsets [136]Single Number [137]Single Number II [169]Majority Element [187]Repeated DNA Sequences [190]Reverse Bits [191]Number of 1 Bits [201]Bitwise AND of Numbers Range [231]Pow

【LeetCode】Single Number (2 solutions)

Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 解法一:用map记录每个元素的次数,返回次数为1的元素 cl

【LeetCode】 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4,−1,2,1] has the largest sum = 6. More practice: If you have figu

【leetcode】Minimum Window Substring

问题: 给定两个字符串,S,T,返回S中包含T中所有字符的最短的字串,若不存在,则返回"".时间复杂度为O(n). 例如:S = "ADOBCODEBANC" T = "ABC" 返回BANC 生活场景: 把问题具体化现实化一点.有n层楼,每层楼里放有一个物品,现在老板给你一个物品清单,里面是要你集齐的物品,你可以乘坐电梯,但是电梯只停一次,停在哪一层,就从哪一层开始向楼上搜集物品,至于要在那层停电梯,由你自己选择. 这里我们当然选择即能集齐物品

【LeetCode】- String to Integer (字符串转成整形)

[ 问题: ] Hint:Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes:It is intended for this problem to be specified vaguely (ie, no given input specs). Y

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w