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. ...
随机推荐
- [linux] shellshock
1> Test if the system is vulnerable env X="() { :;} ; echo vulnerable" /bin/sh -c " ...
- IE6/7常用的hack
hack基础: IE6: _selector{property:value;} selector{property:value;property:value !important;} //IE6 不支 ...
- STM32学习笔记(六) SysTick系统时钟滴答实验(stm32中断入门)
系统时钟滴答实验很不难,我就在面简单说下,但其中涉及到了STM32最复杂也是以后用途最广的外设-NVIC,如果说RCC是实时性所必须考虑的部分,那么NVIC就是stm32功能性实现的基础,NVIC的难 ...
- 如何管理linux开机自启服务
如何管理linux开机自启服务? 自启动服务非常重要,例如 (1)需要手动添加希望自启的服务,如安装svn后没有自动添加,就需要我们手动加入(2)安装某些程序后,自动加到自启动了,但我们不需要,需要手 ...
- nodejs的第一天学习笔记
一. js的模块化 什么是模块化: 模块化的概念最早是后台,随着ajax技术的兴起,js在编程中所占的地位越来越高,同时js的文件也相应的越来越多.为了方便文件的管理和更新,提出了js文件的模块 化, ...
- 转帖-[教程] Win7精简教程(简易中度)2016年8月-0day
[教程] Win7精简教程(简易中度)2016年8月 0day 发表于 2016-8-19 16:08:41 https://www.itsk.com/thread-370260-1-1.html ...
- 1 Spring MVC 原理
1. 注解式 Spring MVC 响应流程: 重要的接口和类的简单说明: DispatcherServlet:前端控制器,用于接收请求. HandlerMapping接口:用于处理请求的映射. D ...
- laravel 表单验证 正则匹配
判断url地址 是否为正确格式 控制器中 $this -> validate($request,[ 'linkname' => 'required|max:6|min:2', 'url' ...
- 关于tomcat会在url末尾自动追加斜杠(/)
今天,突然发现一个问题, 比如我的请求路径为 http://ip:port/my_project/myapp, 在浏览器中敲入这个地址,然后会显示 http://ip:port/my_project ...
- Poisson回归模型
Poisson回归模型也是用来分析列联表和分类数据的一种方法,它实际上也是对数线性模型的一种,不同点是对数线性模型假定频数分布为多项式分布,而泊松回归模型假定频数分布为泊松分布. 首先我们来认识一下泊 ...