c*****y 发帖数: 90 | 1 Given a integer, output its previous and next neighbor number which has
the same number of bit 1 in their binary representation.
下面为什么去判断(number & 3) != 2?
while ((number & 3) != 2) { // for right neighbor, change this line to
// (number & 3) != 1 |
a**x 发帖数: 154 | 2 find the lowest 01 pattern and swap it to find the bigger neighbor number,
while find the lowest 10 pattern and swap it to find the smaller neighbor
number |
c*****y 发帖数: 90 | 3 我开始也同意这个解法,可是发现有的例子用这个方法得不到正确答案。比如0101,按
理它的下一个同样多1的数是0110,可是按这个方法却是1001。另外为什么用那个&3?
我觉得应该是find the lowest 0 and lowest 1, swap them.
number,
neighbor
【在 a**x 的大作中提到】 : find the lowest 01 pattern and swap it to find the bigger neighbor number, : while find the lowest 10 pattern and swap it to find the smaller neighbor : number
|
g*******y 发帖数: 1930 | 4 swap 01 成为10还不够吧
比如
011100
如果swap第一个01,就成了101100
但是正确答案应该是 100011
其实呢,就是swap lowest 01后,应该对后面那截的数做一个简单的0,1排序(尽可能
把后面的所有1都扔到低位)
【在 a**x 的大作中提到】 : find the lowest 01 pattern and swap it to find the bigger neighbor number, : while find the lowest 10 pattern and swap it to find the smaller neighbor : number
|
b****j 发帖数: 78 | 5 这个也是错的,10会变成01。
【在 c*****y 的大作中提到】 : 我开始也同意这个解法,可是发现有的例子用这个方法得不到正确答案。比如0101,按 : 理它的下一个同样多1的数是0110,可是按这个方法却是1001。另外为什么用那个&3? : 我觉得应该是find the lowest 0 and lowest 1, swap them. : : number, : neighbor
|
z***e 发帖数: 5393 | 6 你在careercup上也这个id?
【在 g*******y 的大作中提到】 : swap 01 成为10还不够吧 : 比如 : 011100 : 如果swap第一个01,就成了101100 : 但是正确答案应该是 100011 : 其实呢,就是swap lowest 01后,应该对后面那截的数做一个简单的0,1排序(尽可能 : 把后面的所有1都扔到低位)
|
g*******y 发帖数: 1930 | 7 嗯,有时候也匿名。不过以前还认真回复一下,后来基本上就是偶尔去灌一下水玩玩了
。还是喜欢咱买买提待闺版,呵呵
【在 z***e 的大作中提到】 : 你在careercup上也这个id?
|
k***e 发帖数: 556 | 8 这道题有点复杂
1. are we focusing on unsigned integers now?
if so, then for (1)_2, (11)_2, (111)_2, we cannot find numbers smaller than
them with equal no of 1's.
2. are we focusing on signed integers?
if so, are we using the complementary representation, or we just count the
1's in abs(x)? how about overflow? anyway, it is messy.
【在 c*****y 的大作中提到】 : Given a integer, output its previous and next neighbor number which has : the same number of bit 1 in their binary representation. : 下面为什么去判断(number & 3) != 2? : while ((number & 3) != 2) { // for right neighbor, change this line to : // (number & 3) != 1
|
k***e 发帖数: 556 | 9 那里傻x老印太多
上次有道题要用suffix tree才能做,我就写了一下大概的方法
结果一个老印居然鄙视我 说应该写出code来
无语中
【在 g*******y 的大作中提到】 : 嗯,有时候也匿名。不过以前还认真回复一下,后来基本上就是偶尔去灌一下水玩玩了 : 。还是喜欢咱买买提待闺版,呵呵
|