Description

Count how many 1 in binary representation of a 32-bit integer.

Example

Given 32, return 1

Given 5, return 2

Given 1023, return 9

Challenge

If the integer is n bits with m 1 bits. Can you do it in O(m) time?

解题:很简单,但是要考虑范围的问题。代码如下:

public class Solution {
/*
* @param num: An integer
* @return: An integer
*/
public int countOnes(int num) {
// write your code here
int count = 0;
long temp = (long)num;
if(temp < 0){
temp = (long)Math.pow(2,32) + temp;
}
while(temp != 0){
if(temp %2 == 1)
count++;
temp = temp / 2;
}
return count;
}
};

最新文章

  1. 清除打印机队列中无法清除的任务 &amp; 清空打印池
  2. makefile学习小结
  3. VC++ 限制窗口的大小范围的方法
  4. Linux 下编译安装软件,找不到共享库 xx.so 的解决办法
  5. BZOJ 3155: Preprefix sum
  6. Andorid-Fragment生命周期
  7. 真机測试时的错误:No matching provisioning profiles found
  8. python 内存泄露的诊断 - 独立思考 - ITeye技术网站
  9. python中openpyxl的用法【安装,以及一些基本的操作】
  10. cdh版本的hive安装以及配置
  11. day04 流程控制
  12. k8s运行容器之Job(四)--技术流ken
  13. 执行一个内容为SQL语句的字符串
  14. win7 Host文件修改后无效的解决办法
  15. 在 Linux 中自动配置 IPv6 地址
  16. 随机森林RandomForest
  17. python:turtle绘图模块
  18. vue/cli 3.0 脚手架【进阶】 使用 amfe-flexible 和 postcss-px2rem进行移动端适
  19. 浅谈 [Ljava.lang.Object 异常
  20. Spring @Conditional简单使用 以及 使用时注意事项一点

热门文章

  1. 时钟晶振32.768KHz为什么是15分频?
  2. java 中的懒汉单例和饿汉单例模式
  3. mysql where 加 多个 或者条件
  4. 不用循环,、es6创建一个长度为100的数组
  5. ssm框架基础搭建
  6. maven中的坐标和仓库
  7. Cornerstone|SVN
  8. iOS 11.2 - 11.3.1 越狱教程
  9. Eclipse中html/js/jsp代码的自动联想
  10. h5开发中所遇到的兼容性及所遇到的常见问题