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 should return 3.
这就是考十进制转二进制的题,学到了#include <cstdin>这个头文件,还有简单的转换算法,算法里边数据类型,别随手定义成了int!!!
class Solution {
public:
int hammingWeight(uint32_t n)
{
uint32_t iRet = ;
uint32_t flag = ;
uint32_t iRes = n;
uint32_t iLeft = ;
while(flag != )
{
flag = iRes / ;
iLeft = iRes - flag * ;
if(iLeft == )
{
++iRet;
}
iRes = flag;
}
return iRet;
}
};
写的不好,但多code多学习总是有用。
LeetCode 191:number of one 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 know ...
- leetcode:Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告
题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- [LeetCode] 762. Prime Number of Set Bits in Binary Representation_Easy
Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...
- LeetCode算法题-Number of 1 Bits(Java实现)
这是悦乐书的第186次更新,第188篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第45题(顺位题号是191).编写一个带无符号整数的函数,并返回它所具有的"1 ...
- LeetCode(476): Number Complement
Given a positive integer, output its complement number. The complement strategy is to flip the bits ...
- LeetCode OJ:Number of Islands(孤岛计数)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- LeetCode 693 Binary Number with Alternating Bits 解题报告
题目要求 Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits w ...
- Leetcode 762. Prime Number of Set Bits in Binary Representation
思路:动态规划.注意1024*1024>10^6,所以质素范围是(0,23). class Solution { public int countPrimeSetBits(int L, int ...
随机推荐
- 「日常训练」 不容易系列之(3)—— LELE的RPG难题 (HDU 2045)
题目简述 有排成一行的n" role="presentation">nn个方格,用红(Red).粉(Pink).绿(Green)三色涂每个格子,每格涂一色,要求任何 ...
- 第六篇 常用请求协议之post put patch 总结
[转]https://blog.csdn.net/sshfl_csdn 感谢愿意总结分享的人,thanks idempotent 幂等的 如果一个方法重复执行多次,产生的效果是一样的,那就是i ...
- ADB常用指令
adb 命令是adb程序自带的一些命令:adb shell则是调用Android系统的命令,Android系统特有的命令都放在Android设备的/system/bin目录中 MonkeyRunner ...
- 小心!FOMO3D的坑
null 01 前方高能 近日,区块链机构安比(SECBIT)实验室审计后确认,FOMO3D游戏的智能合约存在随机数漏洞可被利用,FOMO3D合约及所有抄袭源码的山寨合约均存在该安全漏洞. 原本设计上 ...
- HashMap和Hashtable的区别(转载)
转载声明:转载自原文http://www.importnew.com/7010.html HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是 ...
- PAT 甲级 1006 Sign In and Sign Out
https://pintia.cn/problem-sets/994805342720868352/problems/994805516654460928 At the beginning of ev ...
- EXEL文件转成简书MD表格
EXEL文件转成简书MD表格 0.1.3 mac: https://github.com/fanfeilong/exceltk/blob/master/pub/exceltk.0.1.3.pkg wi ...
- 列数不固定时怎么使用el-tabel展示数据
<el-table :data="contents" stripe> <el-table-column v-for="(item, index) in ...
- Maven中如何将源码之外的文件打包及添加本地jar
<build> <resources> <resource> <directory>src/main/resources</directory&g ...
- n元线性方程非负整数解的个数问题
设方程x1+x2+x3+...+xn = m(m是常数) 这个方程的非负整数解的个数有(m+n-1)!/((n-1)!m!),也就是C(n+m-1,m). 具体解释就是m个1和n-1个0做重集的全排列 ...