LeetCode题解之Number of 1 Bits

1、题目描述

2.问题分析

使用C++ 标准库的 bitset 类,将整数转换为 二进制,然后将二进制表示转换为字符串,统计字符串中 1 的个数即可。

3、代码

 1 int hammingWeight(uint32_t n) {
 2         bitset<32> b(n);
 3         string b_s = b.to_string() ;
 4
 5         int count_one = 0;
 6         for(string::iterator it = b_s.begin(); it != b_s.end() ; ++it )
 7         {
 8             if( *it == ‘1‘)
 9                 ++count_one;
10         }
11         return count_one;
12     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/9295660.html

时间: 2024-10-08 15:38:22

LeetCode题解之Number of 1 Bits的相关文章

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

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

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

Leetcode - 693. 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. 解题思路: 题设要求判断形如101010的数字,那么如何在复杂度最小的情况下给出算法呢 首先看一下用python解决本题有哪些基本工具. 说到二进制,首先想到的是位运算符号,python的位运算符号有& ^ ~ | >> &

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题解||Palindrome Number问题

problem: Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of

Leetcode 762. Prime Number of Set Bits in Binary Representation

思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). 1 class Solution { 2 public int countPrimeSetBits(int L, int R) { 3 Set<Integer> prime = new HashSet<Integer>(Arrays.asList(2,3,5,7,11,13,17,19,23)); 4 int[] count = new int[1+R]; 5 int res = 0; 6 for

leetCode题解之Number of Lines To Write String

1.题目描述 2.分析 使用一个map将字母和数字对应起来,方便后续使用. 3.代码 1 vector<int> numberOfLines(vector<int>& widths, string S) { 2 map<char,int> m; 3 vector<int> ans; 4 5 for( int i = 0; i< 26;i++) 6 m[i+'a'] = widths[i]; 7 8 int lines = 1; 9 int cu

2016.5.16——leetcode:Number of 1 Bits ,

leetcode:Number of 1 Bits 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n-1)[n = n & (n-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 exam