代碼
提交代碼
function getGroup(data, index = 0, group = []) {//生成全排列
var need_apply = new Array();
need_apply.push(data[index]);
for(var i = 0; i < group.length; i++) {
need_apply.push(group[i] + data[index]);
}
group.push.apply(group, need_apply);
if(index + 1 >= data.length) return group;
else return getGroup(data, index + 1, group);
}
onmessage = function(message){//監(jiān)聽主線程的數據請求
var msg = message.data;
if(msg == "") postMessage("請輸入正確的字符串");
else {
var data = msg.split("");//將字符串轉數組
postMessage(getGroup(data));
}
}
運行結果