{"id":173,"date":"2021-07-19T15:00:27","date_gmt":"2021-07-19T07:00:27","guid":{"rendered":"https:\/\/aisumura.net\/blog\/?p=173"},"modified":"2021-07-19T15:08:18","modified_gmt":"2021-07-19T07:08:18","slug":"%e5%af%a6%e4%bd%9c%e9%81%9e%e8%bf%b49-%e6%8e%92%e5%88%97","status":"publish","type":"post","link":"https:\/\/aisumura.net\/blog\/2021\/07\/19\/%e5%af%a6%e4%bd%9c%e9%81%9e%e8%bf%b49-%e6%8e%92%e5%88%97\/","title":{"rendered":"\u5be6\u4f5c\u905e\u8ff4(9) &#8211; \u6392\u5217"},"content":{"rendered":"\n<p>\u525b\u597dLeetcode\u6709\u4e00\u6a23\u7684\u984c\u76ee <a href=\"https:\/\/leetcode.com\/problems\/permutations\/\">46. Permutations<\/a> \uff0c<br>\u5728\u6c92\u4e0a\u904e\u96e2\u6563\u6578\u5b78\u4e4b\u524d\u771f\u7684\u5b8c\u5168\u6c92\u982d\u7dd2\uff0c\u76f4\u63a5skip\u6389\uff0c<br>\u73fe\u5728\u4e00\u5e74\u904e\u53bb\u4e86\uff0c\u6709\u96e2\u6563\u8ddf\u8cc7\u7d50\u7b97\u6cd5\u7684\u57fa\u790e\uff0c\u89e3\u8d77\u4f86\u8f15\u9b06\u5f88\u591a\u3002<\/p>\n\n\n\n<h4>\u89e3\u984c\u601d\u8def<\/h4>\n\n\n\n<p>\u6392\u5217n\u500b\u76f8\u7570\u7269\uff0c\u5373n!\u7a2e\u6392\u6cd5\uff0c<br>\u53ef\u4ee5\u5f97\u77e5\u6392\u6cd5\u7684\u7e3d\u6578\u70ba<br>n! = (n &#8211; 1)! * n<br>1! = 1<br><br>\u4f46\u8981\u628a\u6bcf\u7a2e\u6392\u6cd5\u5217\u8209\u51fa\u4f86\uff0c\u5c31\u5fc5\u9808\u66f4\u4ed4\u7d30\u63a8\u6572\uff0c<br>\u4ee5\u984f\u8272\u4e0d\u540c\u7684\u7403\u8209\u4f8b\uff0c<br>\u6392\u5e8f\u4f9d\u5de6\u81f3\u53f3\u7684\u7d05\u9ec3\u7da0\u4e09\u9846\u7403\uff0c\u53ef\u5c07\u7d05\u7403\u62bd\u51fa\uff0c\u5269\u4e0b\u5169\u9846\u7403\u6709:<br>1. \u9ec3\u5de6\u7da0\u53f3<br>2. \u9ec3\u53f3\u7da0\u5de6<br>\u90192\u7a2e\u6392\u6cd5\uff0c<br>\u6700\u5f8c\u628a\u7d05\u7403\u653e\u56de\u53bb\uff0c\u6703\u8b8a\u6210<br>1. \u7d05\u9ec3\u7da0<br>2. \u7d05\u7da0\u9ec3<br>\u90192\u7a2e\u6392\u6cd5\uff0c<br>\u4e4b\u5f8c\u5c07\u9ec3\u7403\u3001\u7da0\u7403\u7528\u4e00\u6a23\u7684\u65b9\u6cd5\u53d6\u51fa\uff0c\u53ef\u4ee5\u5f97\u5230<br>3. \u9ec3\u7d05\u7da0<br>4. \u9ec3\u7da0\u7d05<br>5. \u7da0\u7d05\u9ec3<br>6. \u7da0\u9ec3\u7d05<br>\u7e3d\u5171\u53ef\u4ee5\u5f973 * 2 = 6 = 3!\u7a2e\u6392\u5217\u65b9\u5f0f\uff0c<br>\u4ee5\u6b64\u985e\u63a8\uff0c\u6392\u7b2cn\u500b\u76f8\u7570\u7269\u6642\uff0c<br>\u53ea\u9808\u5c07n\u500b\u76f8\u7570\u7269\u8f2a\u6d41\u62bd\u8d70\uff0c<br>\u6392\u5217\u5b8c\u5f8c\u518d\u653e\u56de\u53bb\uff0c<br>\u5c31\u53ef\u4ee5\u5f97\u5230\u6bcf\u4e00\u7a2e\u7684\u6392\u5217\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h4>\u689d\u4ef6\u6b78\u7d0d<\/h4>\n\n\n\n<p>\u5047\u8a2d\u8981\u6392\u7684\u6578\u5b57\u6709n\u500b\uff0c<br>\u7576n == 1\u6642\uff0c\u56de\u50b3\u81ea\u8eab\uff0c<br>\u7576n &gt; 1\u6642\uff0c\u8f2a\u6d41\u5c07\u7b2ci\u500b\u6578\u5b57&#8221;\u56fa\u5b9a&#8221;\u4f4f\uff0c<br>\u4e26\u5c07\u5176\u4ed6\u5269\u9918\u7684\u6578\u5b57\u505a\u6392\u5217\u7d44\u5408\u5f8c\u7684\u7d50\u679c\u52a0\u4e0a\u53bb\uff0c<br>\u5373\u70ba\u7576\u4e0b\u7684\u89e3\u3002<\/p>\n\n\n\n<h4>Golang<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">package main\n\nimport \"fmt\"\n\n\/\/\u5c07\u9663\u5217\u5f80\u5f8c\u63a81\u683c\uff0c\u85c9\u6b64\u9054\u5230\u5404\u6578\u5b57\u4f9d\u5e8f\u7576\u982d(\u7b2c1\u500b\u4f4d\u7f6e)\u7684\u6548\u679c\nfunc push_back(nums []int) []int {\n    tmp := nums[0]\n    for i := 0; i &lt; len(nums)-1; i++ {\n        nums[i] = nums[i+1]\n    }\n    nums[len(nums)-1] = tmp\n    return append(nums)\n}\n\n\/\/\u6392\u5217\u7d44\u5408\nfunc permute(nums []int) [][]int {\n    if len(nums) == 1 {\n        return [][]int{nums}\n    }\n\n    if len(nums) == 2 {\n        return [][]int{nums, swap(nums)}\n    }\n\n    var result [][]int\n    for i := 0; i &lt; len(nums); i++ {\n        \/\/\u9663\u5217\u9996\u7684\u6578\u5b57\u56fa\u5b9a\uff0c\u5176\u9918\u7684\u6578\u5b57\u7528\u905e\u8ff4\u505a\u6392\u5217\n        prev_result := permute(nums[1:])\n        for j := 0; j &lt; len(prev_result); j++ {\n            \/\/\u628a\u5176\u9918\u6578\u5b57\u6392\u5217\u7684\u7d50\u679c\u52a0\u4e0a\u7576\u4e0b\u56fa\u5b9a\u7684(\u9663\u5217\u9996)\u7684\u6578\u5b57\u5373\u70ba\u6240\u6c42\n            prev_result[j] = append([]int{nums[0]}, prev_result[j]...)\n            result = append(result, prev_result[j])\n        }\n        nums = push_back(nums)\n    }\n    return result\n}\n\nfunc main() {\n    var length int\n    fmt.Println(\"Enter the number of inputs\")\n    fmt.Scanln(&amp;length)\n    fmt.Println(\"Enter the inputs\")\n    nums := make([]int, length)\n    for i := 0; i &lt; length; i++ {\n        fmt.Scanln(&amp;nums[i])\n    }\n    fmt.Printf(\"permute(%d) = \\n\", nums)\n    fmt.Println(permute(nums))\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u525b\u597dLeetcode\u6709\u4e00\u6a23\u7684\u984c\u76ee 46. Permutations \uff0c\u5728\u6c92\u4e0a\u904e\u96e2\u6563\u6578\u5b78\u4e4b\u524d\u771f\u7684\u5b8c\u5168\u6c92\u982d\u7dd2\uff0c\u76f4\u63a5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[6],"_links":{"self":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/173"}],"collection":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":2,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"predecessor-version":[{"id":176,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/posts\/173\/revisions\/176"}],"wp:attachment":[{"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/aisumura.net\/blog\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}