LintCode Count 1 in Binary
知识点
1. 整数的二进制表示法
2. 十进制和二进制的转换
http://baike.baidu.com/view/1426817.htm
3. 负整数的表示(原码,补码,反码)
http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html
4. 位操作 Bit Operation
左移 Left Shift <<
右移 Right Shift >>
与 And &
或 Or |
非 Neg ^
public int countOnes(int num) {
int numOfOne = 0;
int mask = 0;
for(int i = 0; i < 32; i++){
mask = (1 << i);
if((mask & num) != 0){
numOfOne++;
}
}
return numOfOne;
}
LintCode Count 1 in Binary的更多相关文章
- 365. Count 1 in Binary【LintCode java】
Description Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return ...
- lintcode :Count 1 in Binary 二进制中有多少个1
题目: 二进制中有多少个1 49% 通过 计算在一个 32 位的整数的二进制表式中有多少个 1. 样例 给定 32 (100000),返回 1 给定 5 (101),返回 2 给定 1023 (111 ...
- Lintcode: Count of Smaller Number
Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 1 ...
- [LintCode] Serialize and Deserialize Binary Tree(二叉树的序列化和反序列化)
描述 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉 ...
- Count 1 in Binary
Count how many 1 in binary representation of a 32-bit integer. Example Given 32, return 1 Given 5, r ...
- [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LintCode] Count and Say 计数和读法
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- LintCode Maximum Depth of Binary Tree
1 /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, ...
- Lintcode: Remove Node in Binary Search Tree
iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...
随机推荐
- Python学习笔记(0)
Python 是什么类型的语言 Python是脚本语言 Python下载地址:https://www.python.org/downloads/ Python版本:Python 3.4.2 - 64b ...
- [bzoj3555]企鹅QQ(hash)
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 1645 Solved: 616[Submit][Statu ...
- Android Fragment是什么
Fragment是Activity中用户界面的一个行为或者一个部分.你可以在一个单独的Activity上把多个Fragment组合成一个多区域的UI,并且可以在多个Activity中使用.你可以认为F ...
- win10离线安装.net framework 3.5
1.挂载win10系统镜像 2.执行命令 Dism /online /enable-feature /featurename:NetFX3 /All /Source:I:\sources\sxs /L ...
- Android复习指南
基础无外乎几部分:语言(C/C++或java),操作系统,TCP/IP,数据结构与算法,再加上你所熟悉的领域.这里面其实有很多东西,各大面试宝典都有列举. 在这只列举了Android客户端所需要的和我 ...
- 运行Maven是报错:No goals have been specified for this build
No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in t ...
- MTF(Move-to-front transform)数据转换
1.什么是MTF MTF(move-to-front)是一种数据编码方式,用于提高数据压缩技术效果. 在数据压缩算法中,MTF可以作为一个额外的步骤.也就是说 ,可以先进行MTF编码,在进行数据压缩. ...
- SAP的物料归档
我们在对前台对物料进行删除时,是物理删除,也就是打个删除标志,并没有正真的从数据库里删除,在前台还是可以看到的,下面介绍一下SAP的归档处理可以 把已删除的物料在前台删除掉,注意:项目里根据情况得到领 ...
- 【转】CentOS 6.5安装pyspider过程记录
原文地址:http://blog.sina.com.cn/s/blog_48c95a190102wczx.html 1.根据pyspider官方推荐的安装方法,使用pip命令直接安装pyspider ...
- .NET日常总结
属性: AccepButton:按钮在按回车键时执行(确定). CancleButton:按钮在按ESC时执行(取消). MinimizeBox:用于设置窗体上是否会出现最小化按钮. Maximize ...