191. Number of 1 Bits(LeetCode)

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 class Solution {
2 public:
3     int hammingWeight(uint32_t n) {
4          bitset<64> bs(n);
5           return bs.count();
6
7     }
8 };
时间: 2024-08-04 10:06:37

191. Number of 1 Bits(LeetCode)的相关文章

LeetCode 191 Number of 1 Bits(1 比特的数字们)

翻译 写一个函数获取一个无符号整型数,并且返回它的"1"比特的数目(也被叫做Hamming weight). 例如,一个32位整型数"11",转换成二进制是00000000000000000000000000001011,所以这个函数应该返回3. 原文 Write a function that takes an unsigned integer and returns the number of '1' bits it has (also known as the

LeetCode 191. Number of 1 bits (位1的数量)

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

190. Reverse Bits(leetcode)

Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). Follow up:If this function

119. Pascal&#39;s Triangle II(LeetCode)

Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to use only O(k) extra space? class Solution { public: vector<int> getRow(int rowIndex) { vector<int&

338. Counting Bits &amp;&amp; 191. Number of 1 Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. Follow up: It is very e

(LeetCode)杨辉三角形Pascal&#39;s Triangle

题目如下: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 实现代码如下: public class Solution { public List<List<Integer>> generate(int numRows) { Lis

第15个算法-实现 Trie (前缀树)(LeetCode)

解法代码来源 :https://blog.csdn.net/whdAlive/article/details/81084793 算法来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree 实现一个 Trie (前缀树),包含 insert, search, 和 startsWith 这三个操作. 示例: Trie trie = new Trie(); trie.insert("apple"

不邻接植花(leetcode)

有 N 个花园,按从 1 到 N 标记.在每个花园中,你打算种下四种花之一. paths[i] = [x, y] 描述了花园 x 到花园 y 的双向路径. 另外,没有花园有 3 条以上的路径可以进入或者离开. 你需要为每个花园选择一种花,使得通过路径相连的任何两个花园中的花的种类互不相同. 以数组形式返回选择的方案作为答案 answer,其中 answer[i] 为在第 (i+1) 个花园中种植的花的种类.花的种类用  1, 2, 3, 4 表示.保证存在答案. 示例 1: 输入:N = 3,

LeetCode OJ:Number of 1 Bits(比特1的位数)

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