查看: 2117| 回复: 10
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】7.24-7.31 CareerCup 5.5

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
5.5 Write a function to determine the number of bits required to convert integer A to integer B.


回复解法可以按照以下格式来

【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】



Notice:
1、记得在程序注释中表明自己算法的时间、空间复杂度
2、代码难懂之处加注释
3、每道题目有对应的帖子,除了贴解法,欢迎讨论,集思广益
4、任何未尽之处,欢迎回报名帖提问,我会进一步作出修改



上一篇:【第三轮】7.24-7.31 CareerCup 5.4
下一篇:【第三轮】7.24-7.31 CareerCup 5.6
🔗
aasdfdf 2014-7-25 11:49:32 | 只看该作者
全局:
将a和b异或一下,看有几位不相同,不相同的位为1,就是需要转换的数位,统计一下有几个位是1就是最后结果
  1. int numAtoB (int a, int b)
  2. {
  3.         int count = 0;
  4.         int c = a^b;
  5.         while (c>0)
  6.         {
  7.                 if (c%2==1)
  8.                         count++;
  9.                 c = c>>1;
  10.         }
  11.         return count;
  12. }
复制代码
回复

使用道具 举报

🔗
jyh橘子 2014-7-26 06:57:01 | 只看该作者
全局:
【解题思路】
n = n1^n2;   count # of 1s in n;
【时间复杂度】   
【空间复杂度】O(1)
【gist link】https://gist.github.com/jyhjuzi/4e3df8009a547d751d90
回复

使用道具 举报

🔗
renli3000 2014-7-27 01:34:18 | 只看该作者
全局:
【解题思路】
A xor B, then count how many 1s. Consider the situation that one of A, B is negative.
【时间复杂度】   
【空间复杂度】O(1)
【gist link】https://gist.github.com/Noahsark/9ecf07b461edc2832979
回复

使用道具 举报

🔗
grassgigi 2014-7-27 11:46:49 | 只看该作者
全局:
【解题思路】
XOR two numbers and count how many 1 we have in the result

【时间复杂度】
O(1)

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/chrislukkk/524fd7436421a8602ddb
回复

使用道具 举报

全局:
【解题思路】
XOR the two numbers, the remaining 1 indicates the different bits


【时间复杂度】
O(1)

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/happyWinner/c2b46022699bf802ba1c
回复

使用道具 举报

🔗
bearkino 2014-7-29 17:31:10 | 只看该作者
全局:
【解题思路】
n = n1^n2;   count # of 1s in n;

【时间复杂度】   
O(1)

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/UncleGarden/6e0f55baeb121d3737ea
回复

使用道具 举报

🔗
ivycheung1208 2014-7-30 01:15:07 | 只看该作者
全局:
【解题思路】
count 1's in (A XOR B)
【时间复杂度】
O(1)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/a002e8bfd2546cccbe1f
回复

使用道具 举报

🔗
 楼主| 林微熙 2014-8-3 09:28:50 | 只看该作者
全局:
【解题思路】count 1XOR Requirement
【时间复杂度】O(1)
【空间复杂度】O(1)
【gist link】https://gist.github.com/hilda8519/7caabe3c452880df7b59
回复

使用道具 举报

🔗
bitcpf 2014-8-5 03:55:54 | 只看该作者
全局:
【解题思路】XOR the two numbers, then count the 1 in the result
【时间复杂度】O(1)
【空间复杂度】O(1)
【gist link】https://gist.github.com/bitcpf/a047a1ab454947052b86
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表