2019独角兽企业重金招聘Python工程师标准>>>

Write a function that takes an (unsigned) integer as input, and returns the number of bits that are equal to one in the binary representation of that number.

Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case.

package poj;
public class BitCounting {public static int countBits(int n){String binaryStr = Integer.toBinaryString(n);int count = 0;for(int i=0; i<binaryStr.length(); ++i){if(binaryStr.charAt(i) == '1'){count++;}}return count;}
}
package poj;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;public class BitCountingTest {@Testpublic void testGame() {assertEquals(5, BitCounting.countBits(1234)); assertEquals(1, BitCounting.countBits(4)); assertEquals(3, BitCounting.countBits(7)); assertEquals(2, BitCounting.countBits(9)); assertEquals(2, BitCounting.countBits(10)); }
}

转载于:https://my.oschina.net/u/553266/blog/792602

Codewar - Bit Counting相关推荐

  1. DFS:深入优先搜索 POJ-2386 Lake Counting

    深度优先搜索是从最开始的状态出发,遍历所有可以到达的状态. 因此可以对所有的状态进行操作,或列举出所有的状态. Lake Counting POJ - 2386 Due to recent rains ...

  2. Boring counting HDU - 3518 (后缀数组)

    Boring counting \[ Time Limit: 1000 ms \quad Memory Limit: 32768 kB \] 题意 给出一个字符串,求出其中出现两次及以上的子串个数,要 ...

  3. pat1004. Counting Leaves (30)

    1004. Counting Leaves (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A fam ...

  4. CF990G GCD Counting(树上莫比乌斯反演,分层图,并查集)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 Problem 给定一棵点带权无根树,对于每个 k∈[1,2×105]k\in[1,2\times10 ...

  5. 2019 ACM - ICPC 上海网络赛 E. Counting Sequences II (指数型生成函数)

    繁凡出品的全新系列:解题报告系列 -- 超高质量算法题单,配套我写的超高质量题解和代码,题目难度不一定按照题号排序,我会在每道题后面加上题目难度指数(1∼51 \sim 51∼5),以模板题难度 11 ...

  6. 人群密度估计--Structured Inhomogeneous Density Map Learning for Crowd Counting

    Structured Inhomogeneous Density Map Learning for Crowd Counting https://arxiv.org/abs/1801.06642 针对 ...

  7. 人群密度估计--Leveraging Unlabeled Data for Crowd Counting by Learning to Rank

    Leveraging Unlabeled Data for Crowd Counting by Learning to Rank CVPR2018 https://github.com/xialeil ...

  8. 人群密度估计--Crowd Counting Via Scale-adaptive Convolutional Nerual Network

    Crowd Counting Via Scale-adaptive Convolutional Nerual Network https://arxiv.org/abs/1711.04433v2 Co ...

  9. 人群密度估计--Learning a perspective-embedded deconvolution network for crowd counting

    Learning a perspective-embedded deconvolution network for crowd counting 没有找到代码 本文在人群密度估计这个问题上的创新点: ...

最新文章

  1. 05_传智播客iOS视频教程_类对象的使用
  2. python flask restful入门_Python Flask高级编程之RESTFul API前后端分离精讲
  3. 彻底堕落了一回——大三篇
  4. php图片长宽处理,用php调整图片宽高 | 学步园
  5. 记一次node+react项目发布过程(一)--webpack生产环境打包优化
  6. Ubuntu 16.10(x86) Install WordPress SRV 4.7.1-1-5
  7. k8s之ovs-cni
  8. Linux CentOS服务器时间同步阿里云北京时间
  9. 点击按钮弹出单选列表对话框和加载Webview
  10. 2023年美团科研合作课题公开征集启动
  11. kali 安装星火应用商店(半成品
  12. hive和presto下标准时间格式和时间戳的转化
  13. 关于 RocketMQ:The producer group has been created before, specify another name please.这个报错的解决办法...
  14. strust2-学习(一)框架搭建和简单示例
  15. 文本分析常用R包的安装(Rweibo、wordcloud、tm、tmcn、Rwordseg、Rcharts、xlsx、XLConnect)
  16. 如何刻录Dmg启动光盘
  17. vue指令模式 添加埋点
  18. 程序员提供9个最好的论坛
  19. appium android数字字母切换键,Android自定义键盘:数字键盘和字母键盘
  20. 课程设计-商店管理系统(三)----前端页面的制作(三)

热门文章

  1. D-Bus学习(一):总体介绍
  2. html生成excel附件时格式处理
  3. mysql分库方案_干货 : 常用MySQL分库分表方案
  4. MBP本,完美多分区装双系统图文教程,多分区而不影响苹果系统GUID分区表,图文教程...
  5. Kafka 原理串讲
  6. Rendering Gooey Materials With Multiple Layers
  7. ChemDraw如何绘制动物细胞结构
  8. 登录平台,打开详情页,通过验证码获取s_v_web_id,逆向采集生成xb,从cookies读出cookies,采集数据再上报。【建议收藏】
  9. SAM9X5替换系统启动时米尔科技的Logo进度条
  10. 大数据十年回顾(1):大数据史前的数据库发展