Skip to content

Instantly share code, notes, and snippets.

@8q
Last active October 31, 2019 08:04
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 8q/d20232de55d6ca6a065ec14f5856c731 to your computer and use it in GitHub Desktop.
Save 8q/d20232de55d6ca6a065ec14f5856c731 to your computer and use it in GitHub Desktop.
置換->互換の積
// function decompose(l, i) {
// if (i < l.length) {
// var j = l[i]
// if (i === j) {
// return decompose(l, i + 1)
// } else {
// [l[i], l[j]] = [l[j], l[i]]
// return [[i, j]].concat(decompose(l, i + 1))
// }
// } else {
// return []
// }
// }
function decompose(l) {
let arr = []
for (let i = 0; i < l.length; i++) {
let j = l[i]
if (i === j) continue
[l[i], l[j]] = [l[j], l[i]]
arr.push([i, j])
}
return arr
}
console.log(decompose([4, 5, 1, 2, 0, 3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment