Last active
October 31, 2019 08:04
-
-
Save 8q/d20232de55d6ca6a065ec14f5856c731 to your computer and use it in GitHub Desktop.
置換->互換の積
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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