lintcode-easy-Count 1 in Binary
2016-02-24 13:59
190 查看
Count how many
Example
Given
Given
Given
Challenge
If the integer is n bits with m 1 bits. Can you do it in O(m) time?
不知道怎么做challenge……
1in 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?
不知道怎么做challenge……
public class Solution { /** * @param num: an integer * @return: an integer, the number of ones in num */ public int countOnes(int num) { // write your code here int count = 0; for(int i = 0; i < 32; i++){ if(((num >> i) & 1) == 1) count++; } return count; } };
相关文章推荐
- jaxb工具类
- JavaScript的设计模式经典之代理模式
- Jps介绍以及解决jps无法查看某个已经启动的java进程问题
- CentOS RPM安装MySQL 5.6修改默认密码
- C/C++——求下面数据类型的最大值和最小值: char, short, int, long, float, double, long double和numeric_limits使用
- 探索PinYin4j.jar将汉字转换为拼音的基本用法
- kidd风的IOS日志之多线程(NSThread)的使用
- 微服务实战之微服务介绍
- 算法导论------------桶排序算法之研究
- 在win7/8/10鼠标右键添加“管理员取得所有权”
- QuickSort 递归 分治
- Git用法之分支管理和分支开发工作流
- new (std::nothrow) 与 new
- js刷新页面的几种方法
- cocoapods卡在Updating local specs repositories
- 无限循环的ScrollView,原理以及实现,自动循环!!!
- android layout_weight难点解析
- mysql 忽略主键冲突、避免重复插入的几种方式
- 回首2015,展望2016
- NSData