博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript数组排列组合和洗牌算法Fisher–Yates shuffle
阅读量:6860 次
发布时间:2019-06-26

本文共 895 字,大约阅读时间需要 2 分钟。

hot3.png

//数组乱序    洗牌算法Fisher–Yates shuffleArray.prototype.shuffle = function() {    var input = this;    for (var i = input.length-1; i >=0; i--) {        var randomIndex = Math.floor(Math.random()*(i+1));        var itemAtIndex = input[randomIndex];        input[randomIndex] = input[i];        input[i] = itemAtIndex;    }    return input;}//数组排列组合Array.prototype.permute = function() {    var permArr = [],        usedChars = [];    var input = this;    function main(input){        var i, ch;        for (i = 0; i < input.length; i++) {            ch = input.splice(i, 1)[0];            usedChars.push(ch);            if (input.length == 0) {                permArr.push(usedChars.slice());            }            main(input);            input.splice(i, 0, ch);            usedChars.pop();        }        return permArr    }    return main(input);}

转载于:https://my.oschina.net/u/232595/blog/1605357

你可能感兴趣的文章
C++单例模式
查看>>
bower安装报错”Cannot be run with sudo”解决办法
查看>>
android平台中编写jni模块的方法(3)
查看>>
软件工程网络15结对编程1——四则运算优化
查看>>
进程、应用程序域,线程和上下文之间的关系
查看>>
Spring源码学习之一下载和导入
查看>>
13.使用第三方类实现动画
查看>>
H5在js中向指定的元素添加样式
查看>>
本地通知,UILocalNotification
查看>>
分页---总结
查看>>
前端开发的历史和趋势(转摘阮一峰)
查看>>
Ubuntu 削减非 LTS 支持周期
查看>>
_实用的cms企业后台管理模板
查看>>
菜鸟看Redis(一)
查看>>
matplotlib.pyplot.plot()参数详解
查看>>
||PHP||关于=>和->以及::的用法
查看>>
最短路径问题
查看>>
Yii2中定义自己的Widget
查看>>
Aforge.net识别简易数字验证码问题
查看>>
JVM系列二:GC策略&内存申请、对象衰老
查看>>