📣 独立日限时特惠: VIP通行证立减$68
查看: 1676| 回复: 7
跳转到指定楼层
上一主题 下一主题
收起左侧

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

全局:

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

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

x
5.7 An array A contains all the integers from 0 through n, except for one number which is missing. In this problem, we cannot access an entire integer in A with a single operation. The elements of A are represented in binary, and the only operation we can use to access them is "fetch thejth bit ofAfi]," which takes constant time. Write code to find the missing integer. Can you do it in 0(n) time?


回复解法可以按照以下格式来
【解题思路】
【时间复杂度】
【空间复杂度】
【gist link】
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】



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


上一篇:【第三轮】7.24-7.31 CareerCup 5.6
下一篇:【第三轮】7.24-7.31 CareerCup 5.8
🔗
renli3000 2014-7-27 01:42:52 | 只看该作者
全局:
【解题思路】这道题不错,想了一阵才有思路
In position k, from n % 2^(k+1) to calculate how much zeros suppose to be more than ones if the actual difference is larger than our result, that means we lost a 1, then we add a 1 to result in position k. Otherwise we lost a zero, we just do nothing.
【时间复杂度】O(N)
【空间复杂度】O(1)
【gist link】https://gist.github.com/Noahsark/d3ca3235e815389211b6
回复

使用道具 举报

全局:
【解题思路】
refer to the solution in the book

【时间复杂度】
O(n)

【空间复杂度】
O(logn)

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

使用道具 举报

🔗
grassgigi 2014-7-28 06:30:01 | 只看该作者
全局:
【解题思路】
Starting from the least significant bit, check bits for all numbers in the array:
1. Decide whether the missing number has 0 or 1 in this bit position (by checking number of 0s and 1s in all array integers)

2. Recursively call this function with all the numbers has same bits in that position as the missing number, until all the bits(32 or 64 depends on length of Integer)

3. Reforming the missing numbers by left shifting the result of sub routine and appending 0 or 1 at then end.

【时间复杂度】
O(N)

【空间复杂度】
O(N)

【gist link】
https://gist.github.com/chrislukkk/3e676ac9bfbc8a9ddb43
回复

使用道具 举报

🔗
ivycheung1208 2014-7-30 04:15:51 | 只看该作者
全局:
【解题思路】
1. search from LSB, count the number of 1's and 0's, recursively search for the missing number and appendix 0 or 1 after the previous result, depending on relationship between c0 and c1.
2. same idea, using iteration instead
【时间复杂度】
O(n)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/dd204c15820dbc436bc0
回复

使用道具 举报

🔗
ivycheung1208 2014-7-30 04:16:22 | 只看该作者
全局:
【解题思路】
1. search from LSB, count the number of 1's and 0's, recursively search for the missing number and appendix 0 or 1 after the previous result, depending on relationship between c0 and c1.
2. same idea, using iteration instead
【时间复杂度】
O(n)
【空间复杂度】
O(N)
【gist link】
https://gist.github.com/dd204c15820dbc436bc0
回复

使用道具 举报

🔗
jyh橘子 2014-7-30 06:00:17 | 只看该作者
全局:
【解题思路】
1. for digit i: count 0s and 1s at that digit, if #0>#1, missing a 1,  delete all numbers with 0 at that digits and do this again for other digits
2. recursive solution
【时间复杂度】
O(N)
【空间复杂度】
O(N)
【gist link】 https://gist.github.com/jyhjuzi/a97d5c52083c9438d147
回复

使用道具 举报

🔗
bearkino 2014-7-30 12:16:30 | 只看该作者
全局:
【解题思路】
参考书上

【时间复杂度】
O(N)

【空间复杂度】
O(N)

【gist link】
https://gist.github.com/UncleGarden/88967999c234cd171775
回复

使用道具 举报

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

本版积分规则

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