[CareerCup] 5.5 Number of Converted Bits 转换数字所需的位数
5.5 Write a function to determine the number of bits required to convert integer A to integer B.
EXAMPLE
Input: 31,14
Output: 2
这道题给了我们两个数字A和B,问如果将A转化为B需要变几个位,那么我们很容易想到要用异或来做,因为相同位异或为0,那么为1的为就是不相同的位,总和就是我们要求的结果。那么此题就转化为求异或结果中位‘1’的个数,我们可以用for循环来做,判断异或数的最低位是否为1,若为1就计数器加1,然后将异或数向右移动一位,以此类推直至异或数为0,参见代码如下:
解法一:
class Solution {
public:
int bitConvertNumber(int a, int b) {
int res = ;
for (int c = a ^ b; c != ; c >>= ) {
res += c & ;
}
return res;
}
};
还有一种方法,这里不是将异或数每次向右移动一位,而是用n&(n-1)来清楚最低有效位Least Significant Bit,每清除一个,计数器增1,直到异或数为0停止,参见代码如下:
解法二:
class Solution {
public:
int bitConvertNumber(int a, int b) {
int res = ;
for (int c = a ^ b; c != ; c &= c - ) {
++res;
}
return res;
}
};
Tips:根据之前那篇Single Number III 单独的数字之三,我们知道得到最低有效位Least Significant Bit的方法是n&(-n),而这道题又学习了清除最低有效位Least Significant Bit的方法是n&(n-1),这些小技巧需要我们深刻理解,并在今后的解题中灵活的使用,这才是刷题的精髓所在。。。
[CareerCup] 5.5 Number of Converted Bits 转换数字所需的位数的更多相关文章
- 2016.5.15——leetcode:Number of 1 Bits ,
leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...
- LeetCode 191:number of one bits
题目就是: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (als ...
- 【Leetcode_easy】693. Binary Number with Alternating Bits
problem 693. Binary Number with Alternating Bits solution1: class Solution { public: bool hasAlterna ...
- [LeetCode] Number of 1 Bits 位1的个数
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- 【leetcode】Number of 1 Bits
题目描述: Write a function that takes an unsigned integer and returns the number of '1' bits it has (als ...
- Number of 1 Bits(Difficulty: Easy)
题目: Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also ...
- LeetCode 191 Number of 1 Bits
Problem: Write a function that takes an unsigned integer and returns the number of '1' bits it has ( ...
- LeetCode Number of 1 Bits
原题链接在这里:https://leetcode.com/problems/number-of-1-bits/ 题目: Write a function that takes an unsigned ...
- leetCode191/201/202/136 -Number of 1 Bits/Bitwise AND of Numbers Range/Happy Number/Single Number
一:Number of 1 Bits 题目: Write a function that takes an unsigned integer and returns the number of '1' ...
随机推荐
- IOS开发之功能模块--自定义UITabBarViewController的备用代码
前言:因为常用,所以我就备份到这里,然后如果需要修改,可以根据需求进行相关的更改. @implementation YMTabBarController - (void)viewDidLoad { [ ...
- 使用docker搭建lnmp环境
Docker容器LNMP环境搭建 安装 制作镜像 启动并关联实例 安装 系统环境 硬件型号: ThinkPad T520 系统版本: ubuntu 14.04 CPU: i7 RAM: 8G 添加软件 ...
- android媒体文件扫描
项目中可能有这样的需求:下载或导入.导出的图片.音乐等媒体文件,需要马上能在图库或本地视屏播放器中显示出来,或者要能在媒体数据库中查询到媒体文件的相关信息,这时我们就得主动通知系统扫描新的媒体文件了. ...
- Showing progress bar in a status bar pane
在工具卡显示进度条,原文链接:http://www.codeproject.com/Articles/35/Showing-progress-bar-in-a-status-bar-pane 1.构造 ...
- sql server 2005 32位+64位、企业版+标准版、CD+DVD 下载地址大全 .
企业版DVD SQL Server 2005 Enterprise Edition(支持超大型企业) 32 位DVD: ed2k://|file|cs_sql_2005_ent_x86_dvd.iso ...
- C++ 基本知识
无论父类与子类的析构函数是否是virutal,子类的析构函数都会调用父类的析构函数 调用构造函数是与构造函数顺序相反,先子类后基类,否则如果基类先析构,子类的有些资源已经不存在了,会出错. 在C++中 ...
- 基于元数据的ETL系统
从努力到选择 从实现到设计 从部分到整体 以下是我对DW design的一些想法 下次使用C#来实现一下 ETL中Source 的信息 数据提供形式:DB(ORACLE SQLSE ...
- Linux登录出现modle is unknow
一.问题描述 登录linux系统发现控制台无法登录,即使输入正确用户名和密码,也无法登录,回车看到有一个错误“module is unknow”. 但是,ssh可以正常登录. 二.解决办法 ssh登录 ...
- NOIP2008普及组 题解 -SilverN
T1 ISBN号码 题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符, 其规定格式如“x-xxx-xxxxx-x”,其中符号“-”就是分隔符( ...
- Jenkins遇到问题一:jenkins配置权限不对导致无法登陆或者空白页面解决办法
找到.jenkins/config.xml文件:替换为:1.<authorizationStrategy class="hudson.security.AuthorizationStr ...