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 ...
随机推荐
- spring 读取properties文件--通过注解方式
问题: 需要通过properties读取页面的所需楼盘的名称.为了以后便于修改. 解决: 可以通过spring的 PropertiesFactoryBean 读取properties属性,就不需要自己 ...
- lua敏感词过滤
--过滤敏感词(如果onlyKnowHas为true,表示只想知道是否存在敏感词,不会返回过滤后的敏感词,比如用户注册的时候,我们程序是只想知道用户取的姓名是否包含敏感词的(这样也能提高效率,检测到有 ...
- Go基础篇【第8篇】: 内置库模块 bytes [二]
type Reader ¶ type Reader struct { // 内含隐藏或非导出字段 } Reader类型通过从一个[]byte读取数据,实现了io.Reader.io.Seeker.io ...
- DFS(8)——poj2034Anti-prime Sequences
一.题目回顾 题目链接:Anti-prime Sequences Sample Input 1 10 2 1 10 3 1 10 5 40 60 7 0 0 0 Sample Output 1,3 ...
- Daily Scrum02 12.02
今天是黑色星期一,虽然大家最近被各种大作业压得身心疲惫,但是团队的凝聚力战胜了一切不快. 看看同志们今天的战绩,是不是又有一种充实感油然而生呢??? By Ryan Mao who? Today? T ...
- JavaSE复习(五)网络编程
客户端:java.net.Socket 类表示.创建Socket对象,向服务端发出连接请求,服务端响应请求,两者建立连接开始通信 服务端:java.net.ServerSocket 类表示.创建Ser ...
- ubuntu 和 centOS 的apache设置
更改ubuntu的网站访问根目录: 在sudo gedit /etc/apache2/sites-enabled/000-default,把 DocumentRoot /var/www #这 ...
- Mac下离线安装SDK
背景 之前电脑上使用的是Android Studio,其sdk在Libarey下,最近需要在Eclipse下继续做之前的安卓项目,在配置sdk时eclipse自动选择了之前Android Studio ...
- Spring Boot(一)入门篇
SpringBoot概述 Spring Boot的诞生简化了Spring应用开发,SpringBoot提供对Spring容器.第三方插件等很多服务的管理.对于大部分Spring应用,无论是简单的web ...
- systemPath
<dependency> <groupId>com.aliyun.mns</groupId> <artifactId>aliyun-sdk-mn ...