算法空间/Leetcode/字符串全排列:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第90行: 第90行:
[[文件:Leetcode-字符串全排列-3.png|700px|无|Leetcode-字符串全排列-3]]
[[文件:Leetcode-字符串全排列-3.png|700px|无|Leetcode-字符串全排列-3]]
=== 实现 3 ===
=== 实现 3 ===
<syntaxhighlight lang="javascript">
var permute = function(nums) {
    let res= []
    perm(nums, 0, nums.length - 1,res)
    return res
};
function perm (arr, offset, lastIndex, cup) {
    if (offset === lastIndex) {
        cup.push([...arr])
        return
    }
    for (let i = offset; i <= lastIndex; i++) {
        swap(arr,i, offset)
        perm(arr, offset + 1, lastIndex, cup)
        swap(arr, i, offset)
    }
}
function swap (arr, p, q) {
    let temp = arr[p]
    arr[p] = arr[q]
    arr[q] = temp
}</syntaxhighlight>
=== 结论 ===
=== 结论 ===
[[文件:Leetcode-字符串全排列-4.png|700px|无|Leetcode-字符串全排列-4]]
[[文件:Leetcode-字符串全排列-4.png|700px|无|Leetcode-字符串全排列-4]]