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

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

全局:

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

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

x
本帖最后由 林微熙 于 2014-7-24 15:35 编辑

5.1 You are given two 32-bit numbers, N andM, and two bit positions, i and j. Write a method to insert M into Nsuch that M starts at bit j and ends at bit i. You can assume that the bits j through i have enough space to fit all ofM. That is, ifM= 10011, you can assume that there are at least 5 bits between j and i. You would not, for example, have j-3 and i=2, because M could not fully fit between bit 3 and bit 2.
EXAMPLE:
Input: N = 16000000000, M = 10011, i = 2, j = 6
Output: N = 10001001100

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



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


不知道原组织者是不是有什么事情了
我就冒昧继续贴了
**求大牛们继续抛砖引玉啊

点评

我怎么找不到修改帖子的地方  发表于 2014-7-26 01:43
Example 有误,应该是 N = 10000000000, M = 10011, i = 2, j = 6, output: N = 10001001100  发表于 2014-7-25 22:41

上一篇:刷题的时候用STL含有的container好还是自己想办法好啊?
下一篇:【第三轮】7.24-7.31 CareerCup 5.2
🔗
grassgigi 2014-7-25 11:59:52 | 只看该作者
全局:
【解题思路】
Reset i-j bits in N to 0s
Left shift M to fit the i-j position, then perform OR(|) operation between N and M

【时间复杂度】
O(1) - all operations on integer which has 32 bit length

【空间复杂度】
O(1)

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

使用道具 举报

🔗
jyh橘子 2014-7-25 12:40:07 | 只看该作者
全局:
【解题思路】
1. set the bits between index i and index j of n to zeroes;
2. left shift m    i bits
3. combine cleared n and shifted m by or operation
【时间复杂度】
O(1)
【空间复杂度】
O(1)
【gist link】https://gist.github.com/jyhjuzi/ac6f0b44c4db8b804cc5

点评

I guess ur code is not correct. Try n = 10000000001,m=10011,i=2,j=6. I think the mask should be mask = (allone << (j+1))|~(allone << i);  发表于 2014-8-4 11:37
回复

使用道具 举报

🔗
renli3000 2014-7-26 04:03:35 | 只看该作者
全局:

【解题思路】
To inser ones in M, use & ..00M00... To insert zeros in M, use | ..11M11..
The remaining work is to prepare ..00M00.. and ..11M11..
【时间复杂度】
O(1) - all operations on integer which has 32 bit length

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/Noahsark/24fd12d416eb0cf16c2c
回复

使用道具 举报

全局:
【解题思路】
1. clear the i-th bit  to j-th in N
2. left shift M by i bits
3. OR the results from step 1 and 2

【时间复杂度】
O(1)


【空间复杂度】
O(1)


【gist link】
https://gist.github.com/happyWinner/b8205432cf5317e80108

回复

使用道具 举报

🔗
bearkino 2014-7-28 08:08:14 | 只看该作者
全局:
【解题思路】
1. make a mask to clear from j to i with N
2. move the M to the right place
3. "|" OR the step 1 and step 2, put the 1s in M into N, get the answer

【时间复杂度】
O(1)

【空间复杂度】
O(1)

【gist link】
https://gist.github.com/UncleGarden/808982dd62823f43127f
回复

使用道具 举报

🔗
ivycheung1208 2014-7-29 12:01:42 | 只看该作者
全局:
【解题思路】
1. reset and update N bit by bit
2. reset N using a mask, shift M and insert to N
【时间复杂度】
O(1)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/1c8a3e1d1f8d99da01ec
回复

使用道具 举报

🔗
wilbert 2014-8-2 02:05:33 | 只看该作者
全局:
【解题思路】
bitwise operation, zero bits between i to j in N and left shift M for i bits, then or together
【时间复杂度】
O(1)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/iwilbert/f658d122e171ea6ab848
回复

使用道具 举报

🔗
 楼主| 林微熙 2014-8-3 03:33:04 | 只看该作者
全局:
【解题思路】Clear the bits j through i to N, shift m so lines up with j through i, merge M&N
【时间复杂度】O(1)
【空间复杂度】O(1)
【gist link】https://gist.github.com/hilda8519/c32362e64c608da321ab
回复

使用道具 举报

🔗
bitcpf 2014-8-4 11:40:03 | 只看该作者
全局:
【解题思路】
build a mask that from j+1 to i are zeros, and get part of n whose value in the 1s of mask, then left shift m for i, return part of n | shifted m
【时间复杂度】
O(1)
【空间复杂度】
O(1)
【gist link】
https://gist.github.com/bitcpf/4557650885e95e0e41cb
回复

使用道具 举报

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

本版积分规则

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