- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个这样的数组
[0,2,3]
这个数组可能的洗牌是
[0,2,3], [2,3,0], [3,0,2], [3,2,0], [0,3,2], [2,0,3]
如何获得这些组合?我目前唯一的想法是
n = maximum num of possible combinations, coms = []
while( coms.length <= n )
temp = shuffle( original the array );
if temp is there in coms
return
else
coms.push(temp);
但我不认为这是高效的,因为这里我们盲目地依赖均匀分布的 shuffle 方法。
这个问题是否有其他发现?
最佳答案
首先要注意的是,排列的数量相对于元素的数量增加得非常快(13 个元素 = 60 亿个排列),因此对于足够大的输入,任何生成它们的算法的性能都会下降数组。
要注意的第二件事是,由于排列的数量非常大,将它们存储在内存中的成本很高,因此最好使用生成器来生成排列并在生成它们时对其进行处理。
第三个需要注意的是,递归算法带来的开销很大,所以即使找到了递归的解法,也要争取得到非递归的解法。如果存在递归解决方案,则始终可以获得非递归解决方案,但这可能会增加代码的复杂性。
我已经为您编写了一个基于 Steinhaus–Johnson–Trotter 算法 ( http://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm ) 的非递归实现
function swap(arr, a,b){
var temp = arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
function factorial(n) {
var val = 1;
for (var i=1; i<n; i++) {
val *= i;
}
return val;
}
function permute(perm, func){
var total = factorial(perm.length);
for (var j=0, i=0, inc=1; j<total; j++, inc*=-1, i+=inc) {
for (; i<perm.length-1 && i>=0; i+=inc) {
func.call(perm);
swap (perm, i, i+1);
}
func.call(perm);
if (inc === 1) {
swap(perm, 0,1);
} else {
swap(perm, perm.length-1, perm.length-2);
}
}
}
console.clear();
count = 0;
permute([1,2,3,4,5,6], function(){console.log(this); count++;});
console.log('There have been ' + count + ' permutations');
关于javascript - 尽可能多地打乱数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18681165/
我正在使用下面的随机播放脚本来随机化网站上某些 div 的顺序。 http://james.padolsey.com/javascript/shuffling-the-dom/ 该脚本有效,但仅随机化
我想打乱 HashMap 中的值。我有以下类型的 HashMap Map > trainDataSet = new HashMap>(); 我想打乱 map 中的值。我该怎么做呢? 以下是我的尝试:
这个问题已经有答案了: Shuffle a list of integers with Java 8 Streams API (8 个回答) 已关闭 3 年前。 我有一个单词列表:List words
我必须使用 SQL SELECT 语句返回一些结果。我需要返回两条关于员工的信息,Employee# 和 EmployeeName。 我试过了 SELECT Employee#, EmployeeNa
我有一个网页,我正在尝试打印它。我正在使用引导导航栏。问题是,当我使用 bootstarp 导航栏时,打印预览没有任何意义。我在页面中看到了代码行而不是实际内容。只有当我将引用添加到 bootstra
我用 16 位值加载两个 SSE 128 位寄存器。这些值按以下顺序排列: src[0] = [E_3, O_3, E_2, O_2, E_1, O_1, E_0, O_0] src[1] = [E_
我需要做的是:原始状态: 洗牌后: 第一个 div 中的 Divs 留在那里但会被打乱,同样的情况也会发生在具有相同类的第二个 div 中。要在特定的 div
我有一个列表: milk butter eggs orange juice bananas 如何使用 javascript 随机重新排序列表项? 最佳答案
我有一个大小为 n 的 NSMutableArray urlArray,我想从数组中的元素总数中随机选择其中的 4 个 URL。 但是我不想直接打乱 urlArray,我更喜欢制作一个“indexAr
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 5 年前。 Improve this ques
首先,我的母语不是英语,所以请原谅我的一些错误。 我想对 ArrayList 进行洗牌(没问题),但是洗牌后列表必须满足某些条件。我的第一个方法是创建 if 语句,并在每次为 true 时进行洗牌。但
我想打乱 __m256i vector 的元素。并且有一个内在的 _mm256_shuffle_epi8 做类似的事情,但它不执行跨车道洗牌。 如何使用 AVX2 指令来实现? 最佳答案 有一种方法可
有谁知道改变现有 NSString 或 NSMutableString 字符顺序的现有方法吗?无论如何,我有一个解决方法,但如果有一个现有的方法,那就太好了。 例如,给定字符串@"HORSE",一个方
我是 Objective-C 新手,通过反复试验来学习!如果这个问题有点幼稚,请原谅我。 我创建了一组图像,需要对它们进行随机播放。我已经使用了这里给出的建议: What's the Best Way
我们有一个网络应用程序,希望向潜在客户演示,但我们最好的方法是使用现有数据,以获得完整的体验。当然,我们不想使用应用程序中可见的实际客户名称或地址等来执行此操作。 SQL Server 中有没有一种简
有一个简单的方法来缩小和/或混淆 JS,它存在于我的 django 模板中? 我还在js中使用模板变量和模板标签,因此,标准的uglifyjs或类似的不符合我最初的要求。 有什么事情要做吗? 最佳答案
我是新用户 ArrayList 。我不明白。 如果我使用int[]作为ArrayList项目有错误: The method put(int) is undefined for the type Arr
是否可以随机打乱 Excel 工作表中行或列中的值。有没有相关的函数/宏?我该怎么做? -广告。 最佳答案 嗯...就像简单的解决方案一样,无需编程。例如,您有一列数据 (A): 23 78 12 7
我想打乱我的数组项。像这样: [1,2,3,4].scramble => [2,1,3,4] [1,2,3,4].scramble => [3,1,2,4] [1,2,3,4].scramble =>
我目前正在为 Android 开发内容创建应用程序。它的主要目的是让用户生成一个包含文本和图像条目的列表(最后由 EditText 和 ImageView 表示)。 我通过使用 ViewHolder
我是一名优秀的程序员,十分优秀!