{"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_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_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":[],"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}]}}