找到给定二进制位的所有可能排列的最佳算法
我正在寻找一种最佳算法来找出剩余的所有可能的排列 给定的二进制数。
例如:
二进制数是:........1。算法应该返回剩余的 2^7 剩余二进制数,如 00000001,00000011 等。
谢谢, 萨蒂什
I am looking for an optimal algorithm to find out remaining all possible permutation
of a give binary number.
For ex:
Binary number is : ........1. algorithm should return the remaining 2^7 remaining binary numbers, like 00000001,00000011, etc.
Thanks,
sathish
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
给出的示例不是排列!
排列是对输入的重新排序。
因此,如果输入是 00000001,则 00100000 和 00000010 是排列,但 00000011 不是。
The example given is not a permutation!
A permutation is a reordering of the input.
So if the input is 00000001, 00100000 and 00000010 are permutations, but 00000011 is not.
如果这仅适用于小数字(可能最多 16 位),则只需迭代所有数字并忽略不匹配:
If this is only for small numbers (probably up to 16 bits), then just iterate over all of them and ignore the mismatches:
要找到所有内容,您不会比循环所有数字更好,例如,如果您想循环所有 8 位数字,
如果您需要以二进制输出,那么请查看您使用的语言中的字符串格式选项。
例如
printf("%b",i); //不是 C/C++
计算的标准,基数在大多数语言中应该是无关的。
to find all you aren't going to do better than looping over all numbers e.g. if you want to loop over all 8 bit numbers
if you need to output in binary then look at the string formatting options you have in what ever language you are using.
e.g.
printf("%b",i); //not standard in C/C++
for calculation the base should be irrelavent in most languages.
我将你的问题读为:“给定一些始终设置某些位的二进制数,创建剩余的可能的二进制数”。
例如,给定 1xx1:您想要:1001, 1011, 1101, 1111。
O(N) 算法如下。
假设这些位在掩码 m 中定义。你还有一个哈希值 h。
生成数字< n-1,伪代码:
代码中的想法是遍历从 0 到 n-1 的所有数字,并将预定义位设置为 1。然后通过映射结果来记忆结果数字(如果尚未记忆)数字到正在运行的计数器的值。
h 的键将是排列。作为奖励, h[p] 将包含排列 p 的唯一索引号,尽管您在原始问题中不需要它,但它可能很有用。
I read your question as: "given some binary number with some bits always set, create the remaining possible binary numbers".
For example, given 1xx1: you want: 1001, 1011, 1101, 1111.
An O(N) algorithm is as follows.
Suppose the bits are defined in mask m. You also have a hash h.
To generate the numbers < n-1, in pseudocode:
The idea in the code is to walk through all numbers from 0 to n-1, and set the pre-defined bits to 1. Then memoize the resulting number (iff not already memoized) by mapping the resulting number to the value of a running counter.
The keys of h will be the permutations. As a bonus the h[p] will contain a unique index number for the permutation p, although you did not need it in your original question, it can be useful.
你为什么把事情搞得这么复杂!
它很简单,如下所示:
Why are you making it complicated !
It is as simple as the following:
您可以使用许多排列生成算法,例如以下一个:
来源: http://www.bearcave.com/random_hacks/permute.html
确保根据您的需要调整相关常量(二进制数、7 位等...)
There are many permutation generating algorithms you can use, such as this one:
source: http://www.bearcave.com/random_hacks/permute.html
Make sure you adapt the relevant constants to your needs (binary number, 7 bits, etc...)
如果您确实正在寻找排列,那么下面的代码应该可以。
例如,找到给定二进制字符串(模式)的所有可能排列。
1000 的排列为 1000, 0100, 0010, 0001:
If you are really looking for permutations then the following code should do.
To find all possible permutations of a given binary string(pattern) for example.
The permutations of 1000 are 1000, 0100, 0010, 0001:
如果你打算生成n位的所有字符串组合,那么可以使用回溯来解决问题。
干得好 :
If you intend to generate all the string combinations for n bits , then the problem can be solved using backtracking.
Here you go :