LintCode刷题笔记-- Count1 binary
标签:
位运算
描述:
Count how many 1 in binary representation of a 32-bit integer.
解题思路:
统计一个int型的数的二进制表现形式中1的个数
1.与check power of 2中的解题形式非常相似,同样利用num&(num-1) 的结果来检查num中二进制形式上1的个数,区别在于Check Power of 2 是来检查是否存在1,而这一题主要是检查有几个1。
2. 此题可以利用 num = num&(num-1) 每次进行与运算可以消除一个1,在num等于0之前就可以计算出二进制序列中存在几个1了。
参考代码:
http://www.jiuzhang.com/solutions/count-1-in-binary/
LintCode刷题笔记-- Count1 binary的更多相关文章
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
- 【leetcode刷题笔记】Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 【leetcode刷题笔记】Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LintCode刷题笔记-- Maximal Square
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing a ...
- LintCode刷题笔记-- LongestCommonSquence
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal(JAVA)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- 【leetcode刷题笔记】Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
- LintCode刷题笔记-- PaintHouse 1&2
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The ...
- LintCode刷题笔记-- Maximum Product Subarray
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has ...
随机推荐
- tinyint(1)遇到的坑
一. Mysql查询的boolean结果将输出为0或者1. 比如: select 1=1; 其输出结果为1. 二. 使用一套中间件对kafka消息进行解析为mysql 语句,其中遇到如下的问题, 目标 ...
- ROS urdf和xacro文件详解
视觉标签:visual <visual> <origin xyz="0.0 0.0 0.0" /> <geometry> <box siz ...
- leyou_06_Nginx的自启
1.在linux系统的/etc/init.d/目录下创建nginx文件 vim /etc/init.d/nginx 添加以下内容 #!/bin/sh # # nginx - this script s ...
- jqurey相册放大浏览效果。
/*图片弹窗与切换*/ function honorLayer(){ var honorArray = honorArr(); var $msk = $('.js-mask'),$layer = $( ...
- c语言学习笔记 - 枚举类型
今天学习了c语言的枚举类型的使用,可能是PHP里没使用过,开始看的时候还是觉得有点怪,后来做了下例子才理解,这里做个笔记记录一下. #include <stdio.h> enum anim ...
- Ajax和json一道基本的练习题
关于ajax是javaEE中最基本的操作: 下面是这道题: 基本功能: jsp+servlet+ajax实现用户信息查询,实现无刷新删除 用户信息包括 学号 姓名 出生日期 性别 操作 2017010 ...
- npm 安装删除模块
npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [npm install -g xxx]利用npm安装全局模块xxx: 本地安装时将模块写入pac ...
- global.fun.php
<?php /** 所有公共函数文件*/ /** 序列化*/function _serialize($obj){ return base64_encode(gzcompress( ...
- bzoj 1059 [ZJOI2007]矩阵游戏——匈牙利算法
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1059 发现不管怎么换,同一列的元素还是在同一列,同一行的元素还是在同一行. 所以必要条件是每 ...
- es6模块化规则(一)
抄自阮一峰ECMAScript 6 入门 export命令 模块功能主要由两个命令构成:export和import.export用于规定模块的对外接口,import用于引入其他模块提供的方法功能. 一 ...