Lintcode365 Count 1 in Binary solution 题解

【题目描述】

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

计算在一个 32 位的整数的二进制表示中有多少个1.

【题目链接】

www.lintcode.com/en/problem/count-1-in-binary/

【题目解析】

最容易想到的方法是对数字依次右移,判断每一位是否为1,时间复杂度为o(n),n为数字有效位数,但是这个方法不够简洁。

更优的方法是使用n&(n-1),时间复杂度是o(m),m为1的位数,

具体实现原理可假设一个数为

num = 00010 10000

关注点放在后5位,

num-1 = 0001001111,可观察到,对一个数进行-1操作,会使最后一位1变0,高于最后一位1的位不变,低于最后一位1的位全变1,于是我们可以通过与操作,使原来最后一位1所在位变成0。并通过这个方法来判断总共有多少个1

【参考答案】

www.jiuzhang.com/solutions/count-1-in-binary/

原文地址:https://www.cnblogs.com/qiangqingci/p/8660953.html

时间: 2024-10-14 21:02:27

Lintcode365 Count 1 in Binary solution 题解的相关文章

LeetCode: Maximum Depth of Binary Tree 题解

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://

[Coding Made Simple] Count Number of Binary Tree Possible given Preorder Sequence

Count Number of Binary Tree Possible given Preorder Sequence. For example, given preorder sequence of {10, 11, 9, 12, 13, 14}, the total possible number of binary trees is 42. Solution 1. Recursion Since we are only trying to find possbile binary tre

POJ 2499 Binary Tree 题解

本题使用所谓的辗转相除法. 还需要逆过来遍历二叉树.可以想象给出的数据点是根节点,然后遍历到根节点(1,1). 考的是根据给出的规则,总结规律的能力. #include <stdio.h> namespace BinaryTree2499_1 { int main() { int T, a, b, le, ri; scanf("%d", &T); for (int t = 1; t <= T; t++) { scanf("%d %d", &

Lintcode7 Binary Tree Serialization solution 题解

[题目描述] Design an algorithm and write code to serialize and deserialize a binary tree. Writing the tree to a file is called 'serialization' and reading back from the file to reconstruct the exact same binary tree is 'deserialization'. 设计一个算法,并编写代码来序列化

Lintcode11 Search Range in Binary Search Tree solution 题解

[题目描述] Given two values k1 and k2 (where k1 < k2) and a root pointer to a Binary Search Tree. Find all the keys of tree in range k1 to k2. i.e. print all x such that k1<=x<=k2 and x is a key of given BST. Return all the keys in ascending order. 给

Lintcode70 Binary Tree Level Order Traversal II solution 题解

[题目描述] Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). 给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历) [题目链接] www.lintcode.com/en/problem/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 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

leetcode Balanced Binary Tree 题解

题目描述 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 思路 判断一棵树是不是平衡二叉树,递归的求每个结点的左子树和

Lowest Common Ancestor of a Binary Tree题解

这是LeetCode上的一道题,让我们先来看一看题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest nod