Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created May 11, 2015 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashrock/daff7128cb354bb3da58 to your computer and use it in GitHub Desktop.
Save hashrock/daff7128cb354bb3da58 to your computer and use it in GitHub Desktop.
permutation
// pure js version of http://qiita.com/higuma/items/5af4e62bdf4df42ce673
function perm(result, l, r, n){
if(n > 0){
r.forEach(function(item, i){
var rest = r.slice(0);
var e = rest.splice(i, 1);
perm(result, l.concat(e), rest, n - 1);
})
}else{
result.push(l);
}
}
function perm2(list){
var result = [];
perm(result, [], list, list.length);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment