列表

详情


FED5. 全排列

描述

请补全JavaScript代码,要求以数组的形式返回字符串参数的所有排列组合。
注意:
1. 字符串参数中的字符无重复且仅包含小写字母
2. 返回的排列组合数组不区分顺序

示例1

输入:

_permute('abc')

输出:

['abc','acb','bac','bca','cab','cba']

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1684ms, 内存消耗: 77816KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _permute = string => {\n                // 补全代\n                const res = [];\n                function search(str) {\n                    str.length === string.length && res.push(str);\n                    for(let char of string) {\n                        str.indexOf(char) < 0 && search(str+char);\n                    }\n                }\n                return search('') || res;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1689ms, 内存消耗: 77772KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _permute = string => {\n                // 补全代码\n                if(string.length === 1){\n                    return [string]\n                }\n                const res = []\n                for(let s of string){\n                  const arr = string.split('').filter(str => str !== s) \n                  _permute(arr.join('')).forEach(item => {\n                    res.push(s + item)\n                })\n                }\n               return res\n            }\n \n\n\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1689ms, 内存消耗: 77824KB, 提交时间: 2022-02-08

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n                       // 补全代码\n            const _permute = string => {\n                if(string.length === 1) {\n                    return [string]\n                }\n                const results = []\n                for(let s of string){\n                    const arr = string.split('').filter(str =>str !== s)\n                    _permute(arr.join('')).forEach(item => {\n                        results.push(s + item)\n                    })\n                }\n                return results\n            }\n\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1738ms, 内存消耗: 77800KB, 提交时间: 2022-01-01

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _permute = string => {\n                // 补全代码\n                var result = []\n                if(string.length > 1){\n                    for(let i=0;i<string.length;i++){\n                        var left = string[i];\n                        var rest = string.slice(0,i)+string.slice(i+1,string.length);\n                        var perResult = _permute(rest);\n                        for(let j=0;j<perResult.length;j++){\n                            var temp = left + perResult[j];\n                            result.push(temp);\n                        }\n                    }\n                }else{\n                    result.push(string);\n                }\n                return result\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1739ms, 内存消耗: 77900KB, 提交时间: 2021-12-14

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n    </head>\n    <body>\n    \t\n        <script type=\"text/javascript\">\n            const _permute = string => {\n                // 补全代码\n               let ans = [];\n                if(string.length < 1) return [string]\n                for(let i=0;i < string.length;i++) {\n                    let head = string.slice(i, i + 1)\n                    let tail = string.slice(0, i) + string.slice(i + 1, string.length)\n                    for(let rest of _permute(tail)) {\n                        ans.push(head + rest)\n                    }\n                }\n                return ans\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题