{"id":4857,"date":"2022-03-21T23:17:23","date_gmt":"2022-03-21T15:17:23","guid":{"rendered":"https:\/\/1989lifejourney.com\/?p=4857"},"modified":"2022-03-21T23:30:17","modified_gmt":"2022-03-21T15:30:17","slug":"array-permutation-by-iterative","status":"publish","type":"post","link":"https:\/\/1989lifejourney.com\/?p=4857","title":{"rendered":"Array Permutation\/Combination by Iterative"},"content":{"rendered":"\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>\u53c3\u8003\u5be6\u4f5c:<\/strong> <a href=\"https:\/\/www.youtube.com\/watch?v=zIY2BWdsbFs\">https:\/\/www.youtube.com\/watch?v=zIY2BWdsbFs<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Permutation:<\/h2>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:15px;\"><code>func permutation(_ nums: &#091;Int], _ n: Int) -&gt; &#091;&#091;Int]] {\n    var result: &#091;&#091;Int]] = &#091;]\n    var cur: &#091;Int] = &#091;]\n    var used: &#091;Bool] = Array(repeating: false, count: nums.count)\n    helper(nums, &amp;used, 0, n, 0, &amp;cur, &amp;result)\n    return result\n}\n\n\nfunc helper(_ nums: &#091;Int], _ used: inout &#091;Bool], _ depth: Int, _ n: Int, _ startIndex: Int, _ cur: inout &#091;Int], _ result: inout &#091;&#091;Int]]) {\n    if depth == n {\n        result.append(cur)\n        return\n    }\n    \n    for i in startIndex ..&lt; nums.count {\n        if !used&#091;i] {\n            used&#091;i] = true\n            cur.append(nums&#091;i])\n            helper(nums, &amp;used, depth + 1, n, i + 1, &amp;cur, &amp;result)\n            cur.removeLast()\n            used&#091;i] = false\n        }\n    }\n}\n\nprint(permutation(&#091;1,2,3], 1))\nprint(permutation(&#091;1,2,3], 2))\nprint(permutation(&#091;1,2,3], 3))<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Combination:<\/h2>\n\n\n\n<pre class=\"wp-block-code\" style=\"font-size:15px;\"><code>func combination(_ nums: &#091;Int], _ n: Int) -&gt; &#091;&#091;Int]] {\n    var result: &#091;&#091;Int]] = &#091;]\n    var cur: &#091;Int] = &#091;]\n    helper(nums, 0, n, 0, &amp;cur, &amp;result)\n    return result\n}\n\n\nfunc helper(_ nums: &#091;Int], _ depth: Int, _ n: Int, _ startIndex: Int, _ cur: inout &#091;Int], _ result: inout &#091;&#091;Int]]) {\n    if depth == n {\n        result.append(cur)\n        return\n    }\n    \n    for i in startIndex ..&lt; nums.count {\n        cur.append(nums&#091;i])\n        helper(nums, depth + 1, n, i, &amp;cur, &amp;result)\n        cur.removeLast()\n    }\n}\n\nprint(combination(&#091;1,2,3], 1))\nprint(combination(&#091;1,2,3], 2))\nprint(combination(&#091;1,2,3], 3))<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u53c3\u8003\u5be6\u4f5c: <a href=\"https:\/\/www.youtube.com\/watch?v=zIY2BWdsbFs\" rel=\"nofollow\">https:\/\/www.youtube.com\/watch?v=zIY2BWdsbFs<\/a> Permu [&hellip;]<\/p>\n","protected":false},"author":139426785,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_postgate_lock_override":"inherit","_postgate_lock_mode":"inherit","_postgate_preview_mode":"inherit","_postgate_preview_limit":0,"_postgate_product_ids":[],"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","jetpack_seo_llms_txt_always_include":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_wpcom_ai_launchpad_first_post":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[89600350,1],"tags":[],"post_folder":[],"class_list":["post-4857","post","type-post","status-publish","format-standard","hentry","category-leetcode","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paR5vw-1gl","jetpack-related-posts":[{"id":4099,"url":"https:\/\/1989lifejourney.com\/?p=4099","url_meta":{"origin":4857,"position":0},"title":"\u8e29\u5730\u96f7\u7a0b\u5f0f Swift Code","author":"Kevin","date":"25 1 \u6708, 2022","format":false,"excerpt":"\u7d66\u5b9a\u5217\u6578(row)\u548c\u884c\u6578(col)\u4ee5\u53ca\u5168\u90e8\u7684\u5730\u96f7\u500b\u6578\uff0c\u5beb\u51fa\u8e29\u5730\u96f7\u904a\u6232 \/\/ -1 represent\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4604,"url":"https:\/\/1989lifejourney.com\/?p=4604","url_meta":{"origin":4857,"position":1},"title":"Print all permutation of an array","author":"Kevin","date":"26 2 \u6708, 2022","format":false,"excerpt":"\u984c\u76ee: \u7d66\u5b9a\u4e00\u7d44\u6574\u6578array\uff0c\u5370\u51fa\u6240\u6709\u7684\u6392\u5217\u7d44\u5408 EX: input: [1,2], output\u2026","rel":"","context":"\u5728\u300cLeetCode\u300d\u4e2d","block_context":{"text":"LeetCode","link":"https:\/\/1989lifejourney.com\/?cat=89600350"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4074,"url":"https:\/\/1989lifejourney.com\/?p=4074","url_meta":{"origin":4857,"position":2},"title":"KMP algorithm","author":"Kevin","date":"16 1 \u6708, 2022","format":false,"excerpt":"\u5b57\u4e32\u6bd4\u5c0d\u554f\u984c: \u7d66\u5b9a\u4e00\u500b\u5b57\u4e32(txt)\u548c\u53e6\u4e00\u500b\u5b57\u4e32(pattern)\uff0c\u78ba\u8a8dpattern\u5b57\u4e32\u662f\u5426\u5b58\u5728\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/img.youtube.com\/vi\/V5-7GzOfADQ\/0.jpg?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":4067,"url":"https:\/\/1989lifejourney.com\/?p=4067","url_meta":{"origin":4857,"position":3},"title":"Quick Sort","author":"Kevin","date":"15 1 \u6708, 2022","format":false,"excerpt":"func partition(arr: inout [Int], front: Int, \u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4071,"url":"https:\/\/1989lifejourney.com\/?p=4071","url_meta":{"origin":4857,"position":4},"title":"Merge sort","author":"Kevin","date":"15 1 \u6708, 2022","format":false,"excerpt":"func merge(arr: inout [Int], front: Int, end:\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4134,"url":"https:\/\/1989lifejourney.com\/?p=4134","url_meta":{"origin":4857,"position":5},"title":"System Design Practice &#8211; 1","author":"Kevin","date":"30 1 \u6708, 2022","format":false,"excerpt":"\u984c\u76ee: \u67093\u7a2eRoutine:(1)\u6bcf\u500bRoutine\u6709\u76f8\u540c\u7684\u5c6c\u6027\uff0c\u4f46\u4e5f\u6709\u5404\u5225\u4e0d\u540c\u7684\u5c6c\u6027(2)\u6bcf\u500bR\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/users\/139426785"}],"replies":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4857"}],"version-history":[{"count":7,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4857\/revisions"}],"predecessor-version":[{"id":4867,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/4857\/revisions\/4867"}],"wp:attachment":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4857"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fpost_folder&post=4857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}