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 ...
随机推荐
- MySQL性能分析和优化-part 1
MySQL性能优化 平时我们在使用MySQL的时候,怎么评估系统的运行状态,怎么快速定位系统瓶颈,又如何快速解决问题呢? 本文总结了多年来MySQL优化的经验,系统介绍MySQL优化的方法. OS性能 ...
- [Node] Agenda 中文文档 定时任务调度系统[基础篇]
Agenda简介 使用步骤概述 步骤详述 初始化 定义任务 参数说明: 配置任务 参数说明 设置监听 注意事项 Agenda简介 Agenda是一个定时任务管理模块,它将node中的定时任务存储在数据 ...
- LeetCode 389——找不同
1. 题目 2. 解答 2.1. 方法一 将 s 和 t 转化为 Python 的列表,然后遍历列表 s 的元素,将它们从列表 t 中删除,最后列表 t 中会余下一个元素,即为所求. class So ...
- mysql与hive2.1.1安装和配置
1.mysql安装 这个安装很简单,是在线安装,只需要按顺序执行一下几个命令就ok了. (1)sudo apt-get install mysql-server (2)sudo apt-get ins ...
- Ext.net中TreePanel动态生成
这个问题可以参考官网例子:http://examples2.ext.net/#/TreePanel/Basic/Built_in_CodeBehind/ 贴一段本人程序中用到的动态生成核心代码: Ex ...
- fork开源代码后如何基于某个tag建立自己的branch
应用场景: 在github上fork一个自己想看的开源项目,想基于某个tag来写一些测试demo,然后可以做到版本控制. 方法: //克隆 git clone xxxxx.git //查看tag gi ...
- .Net MVC中使用css 和js
@section script { <script language="javascript"> </script> }
- 多个jar包的合并
1.将所有jar文件复制至某临时目录中,通过jar命令解压得到所有的.class文件 > jar -xvf xx.jar xx.jar必须为具体的jar,不能为*.jar,会报FileNotFo ...
- iMuseum
iMuseum 每日环球展览 iMuseum https://itunes.apple.com/cn/app/%E6%AF%8F%E6%97%A5%E7%8E%AF%E7%90%83%E5%B1%95 ...
- 大数据Hadoop-2
大数据Hadoop学习之搭建Hadoop平台(2.1) 关于大数据,一看就懂,一懂就懵. 大数据的发展也有些年头了,如今正走在风口浪尖上,作为小白,我也来凑一份热闹. 大数据经过多年的发展,有着不同的 ...