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.

题解:利用位运算的知识,n&(n-1)的结果会出去n的最后一位1.比如n=11=1011,n-1=10=1010,那么n&(n-1)=1010,最后一位1就没有了。设置一个循环,每次赋值n=n&(n-1)。n为0的时候统计循环的次数就知道n的二进制里面有多少个1了。

Java代码:

 public class Solution {
// you need to treat n as an unsigned value
public int hammingWeight(int n) {
int sum = 0;
while(n != 0){
sum ++;
n = n&(n-1);
}
return sum;
}
}

【leetcode刷题笔记】Number of 1 Bits的更多相关文章

  1. LeetCode刷题笔记和想法(C++)

    主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...

  2. 18.9.10 LeetCode刷题笔记

    本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...

  3. LeetCode刷题笔记 - 12. 整数转罗马数字

    学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...

  4. LeetCode算法题-Prime Number of Set Bits in Binary Representation(Java实现)

    这是悦乐书的第311次更新,第332篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第180题(顺位题号是762).给定两个正整数L和R,在[L,R]范围内,计算每个整数的 ...

  5. LeetCode算法题-Binary Number with Alternating Bits(Java实现)

    这是悦乐书的第292次更新,第310篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第160题(顺位题号是693).给定正整数,检查它是否具有交替位:即它的二进制数的任意两 ...

  6. Leetcode刷题笔记(双指针)

    1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...

  7. 【leetcode刷题笔记】Excel Sheet Column Number

    Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...

  8. 【leetcode刷题笔记】Letter Combinations of a Phone Number

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  9. 【leetcode刷题笔记】Single Number II

    Given an array of integers, every element appears three times except for one. Find that single one. ...

随机推荐

  1. 导入mysql文件提示“ASCII '\0' appeared in the statement”

    在windows服务器上导入mysql文件时,出现以下报错:ASCII '\0' appeared in the statement, but this is not allowed unless o ...

  2. shiro配置数据库连接池总结

    在项目中要使用shiro做权限认证和登录许可等,现在总结一份,以备以后使用 ms sql版本 [main]ds=com.mchange.v2.c3p0.ComboPooledDataSourceds. ...

  3. Eureka 2.0 闭源--选择Consul???

    在上个月我们知道 Eureka 2.0 闭源了,但其实对国内的用户影响甚小,一方面国内大都使用的是 Eureka 1.X 系列,另一方面 Spring Cloud 支持很多服务发现的软件,Eureka ...

  4. CVS 相关内容

    CVS 基础 cvs 并不是 eclipse 独有, 而是 eclipse 支持 cvs. cvs 是用于几个程序员之间共享程序避免冲突的一个工具, 是 C/S 架构. cvs 是 java 的一个开 ...

  5. asp.net C#实现下载文件的六种方法实例

    protected void Button1_Click(object sender, EventArgs e)  {  /*  微软为Response对象提供了一个新的方法TransmitFile来 ...

  6. PYTHON --WebAPP项目转载(廖雪峰) -- Day 1 -- 搭建开发环境

    Day 1 - 搭建开发环境   搭建开发环境 首先,确认系统安装的Python版本是3.5.x: $ python3 --version Python 3.5.1 然后,用pip安装开发Web Ap ...

  7. C字符串复制

    void mystrcpy(char *from, char *to) { for(; *from != '\0'; from++, to++) { *to = *from; } *to = '\0' ...

  8. linux 大量的TIME_WAIT解决办法(转)

    发现存在大量TIME_WAIT状态的连接tcp        0      0 127.0.0.1:3306              127.0.0.1:41378             TIME ...

  9. Jmeter中中文乱码

    jmeter-察看结果树-响应数据中的中文显示乱码 jmeter\bin\jmeter.properties 默认编码为:ISO-8859-1# The encoding to be used if ...

  10. ofstream和ifstream详细用法

    ASCII和二进制文件的输入输出 First:包含头文件#include <fstream> ASCII输入: 首先要创建一个in-stream对象:ifstream fin(" ...