lc面试准备: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 return 3.
接口
复杂度
3 代码
public int hammingWeight(int n) {
int res = 0;
for (int i = 0; i < 32; i++) {
final int bit = (n >> i) & 1;
res += bit;
}
return res;
}
4 总结
和Reverse Bits思路一样。
5 参考
lc面试准备:Number of 1 Bits的更多相关文章
- [LC] 191. Number of 1 Bits
Write a function that takes an unsigned integer and return the number of '1' bits it has (also known ...
- 762. Prime Number of Set Bits in Binary Representation二进制中有质数个1的数量
[抄题]: Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...
- 191. Number of 1 Bits 二进制中1的个数
[抄题]: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- LN : leetcode 191 Number of 1 Bits
lc 191 Number of 1 Bits 191 Number of 1 Bits Write a function that takes an unsigned integer and ret ...
- [LeetCode] 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 (als ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- LeetCode Number of 1 Bits
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...
随机推荐
- Java Stax操作XML简介
使用stax操作xml 非常的简单,它的读取过程像是一个光标在移动.针对不同的节点做不同的处理. 先看一个基于光标的模型处理xml: public class StaxTest { @Test pub ...
- CentOS允许/禁止ping的方法
一.临时生效 1.允许ping >/proc/sys/net/ipv4/icmp_echo_ignore_all 2.禁止ping >/proc/sys/net/ipv4/icmp_ech ...
- Linux重复执行上一条命令
执行刚刚执行的一条命令: !! 执行最近一个以指定字符串开头的命令(比如man) !man !m 引用上一个命令的最后一个参数 !$ <ESC>, .
- 学习java随笔第四篇:运算符
算术运算符 "+":加法运算符,也可做字符连接用途 "-":减法运算符 "*":乘法运算符 "/":除法运算符 &quo ...
- 查看oracle 启动了多久
想看一下系统正常运行了多少天?开机多长时间没有重启了? windows系统 C:\>systeminfo |find “系统启动时间”系统启动时间: 265 天 4 小时 26 分 32 ...
- DependencyProperty
<Window x:Class="DependencyPropertyDemo.MainWindow" xmlns="http://schemas.microsof ...
- C++语言体系设计哲学的一些随想(未完待续)
对于静态类型语言,其本质目标在于恰当地操作数据,得到期望的值.具体而言,需要: (1)定义数据类型 你定义的数据是什么,是整形还是浮点还是字符.该类型的数据可以包含的值的范围是什么. (2)定义操作的 ...
- a+b(高精度)[陈涛]——c语言
#include<stdio.h>#include<string.h>char s[1000];---------------------------------------- ...
- php日期处理
$datetime=strtotime(date("Y-m-d",time()));//获取当前日期并转换成时间戳 $datetime=$datetime+86400;//在时间戳 ...
- Android学习2--项目文件列表简单分析
使用Eclipse创建的默认项目文件列表如下: src:src目录是Android工程的源程序目录,该目录用于存放Java项目的源代码 gen:gen目录存放所有自动生成的文件,在这个目录中最关键的文 ...