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基础知识强化之集合框架笔记53:Map集合之Map集合的遍历 键值对对象找键和值
1. Map集合的遍历(键值对对象找键和值) Map -- 夫妻对 思路: A: 获取所有结婚证的集合 B: 遍历结婚证的集合,得到每一个结婚证 C: 根据结婚证获取丈夫和妻子 转换: A: ...
- RedHat7搭建PHP开发环境(Zend Studio)
下载Zend Studio # wget http://downloads.zend.com/studio-eclipse/13.0.1/ZendStudio-13.0.1-linux.gtk.x86 ...
- 用php切割大图片为成规则的小图
将根据xml配置,将合并后的大图切割成一系列小图 <?php /** * 将大图片按照配置切割成一定比例的小图片 * 并按照一定规则给小图片命名 * * 使用方法: *根据guardians/g ...
- 第一篇:web之前端之html
前端之html 前端之html 本节内容 前端概述 html结构 标签探秘 <!DOCTYPE html>标签 head标签 body标签 1.前端概述 一个web服务的组成分为前端和 ...
- 如何使用Git上传项目代码到代码服务器
如你本机新建Git项目 地址:git@github.com:yourName/yourRepo.git,远程代码库服务器地址:192.168.10.1,远程代码服务器账户名密码:admin 密码:12 ...
- WisDom.Net 框架设计(八) 持久层
WisDom.Net ---持久层 1.什么是持久层 持久层负责最基础的功能支撑,为项目提供一个高层,统一,和并发的数据持久机制,提供了比如建立数据库连接,关闭数据库连接,执行sql语 ...
- android Activity 生命周期
今天第一次详细学习android,主要了解了一下activity的生命周期,下面详细说一下自己的简介: 在Actity中最主要的有一下几个方法: protectedvoid onCreate(Bund ...
- 一封给“X教授”的回信(讨论Socket通信)
转载:http://www.cnblogs.com/tianzhiliang/archive/2011/03/02/1969187.html 前几天"X教授"发Email与我讨论S ...
- SQL Trainning 总结
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- gulp分享文档
Grunt--I/O操作: 读取A → A.a() → 写出A → 读取A → A.b() → 写出A; gulp--数据流:读取A → A.a() → A.b() → 写出A. Part① 构建gu ...