LintCode: Count 1 in Binary

C++

 1 class Solution {
 2 public:
 3     /**
 4      * @param num: an integer
 5      * @return: an integer, the number of ones in num
 6      */
 7     int countOnes(int num) {
 8         // write your code here
 9         int sum = 0;
10         while (num) {
11             sum ++;
12             num = num&(num-1);
13         }
14         return sum;
15     }
16 };

每次“&”都会去掉num最右边的1.

如果一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。其余所有位将不会受到影响。

举个例子:一个二进制数1100,从右边数起第三位是处于最右边的一个1。减去1后,第三位变成0,它后面的两位0变成了1,而前面的1保持不变,因此得到的结果是1011.我们发现减1的结果是把最右边的一个1开始的所有位都取反了。这个时候如果我们再把原来的整数和减去1之后的结果做与运算,从原来整数最右边一个1那一位开始所有位都会变成0。如1100&1011=1000.也就是说,把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0.那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。

C++,

使用右移操作, -1>>1之后还是-1,也即是说负数需要特殊处理,为了解决这个问题,只需要把int型转换成unsigned int型。

 1 class Solution {
 2 public:
 3     /**
 4      * @param num: an integer
 5      * @return: an integer, the number of ones in num
 6      */
 7     int countOnes(int num) {
 8         // write your code here
 9         unsigned int n = num;
10         int sum = 0;
11         while (n) {
12             sum += n&1;
13             n>>=1;
14         }
15         return sum;
16     }
17 };
时间: 2024-10-16 20:22:24

LintCode: Count 1 in Binary的相关文章

[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

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

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的位数

[LintCode] Remove Node in Binary Search Tree

Remove Node in Binary Search Tree Given a root of Binary Search Tree with unique value for each node.  Remove the node with given value. If there is no such a node with given value in the binary search tree, do nothing. You should keep the tree still

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 10000) and an query list. For each query, give you an integer, return the number of element in the array that are smaller than the given integer. Have

LintCode : Inorder Successor in Binary Search Tree

Very interesting problem! http://www.lintcode.com/zh-cn/problem/inorder-successor-in-binary-search-tree/ Description: Given a binary search tree (See Definition) and a node in it, find the in-order successor of that node in the BST. Example: Given tr

lintcode 中等题:binary tree serialization 二叉树的序列化和反序列化

题目 二叉树的序列化和反序列化 设计一个算法,并编写代码来序列化和反序列化二叉树.将树写入一个文件被称为“序列化”,读取文件后重建同样的二叉树被称为“反序列化”. 如何反序列化或序列化二叉树是没有限制的,你只需要确保可以将二叉树序列化为一个字符串,并且可以将字符串反序列化为原来的树结构. 样例 给出一个测试数据样例, 二叉树{3,9,20,#,#,15,7},表示如下的树结构: 3 / 9 20 / 15 7 我们的数据是进行BFS遍历得到的.当你测试结果wrong answer时,你可以作为输

[LintCode] Count of Smaller Number before itself

Count of Smaller Number before itself Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) . For each element Ai in the array, count the number of element before this element Ai is smaller than it

Lintcode: Search Range in Binary Search Tree

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. Example