{"id":5610,"date":"2019-09-29T20:22:42","date_gmt":"2019-09-30T03:22:42","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5610"},"modified":"2019-09-29T20:23:08","modified_gmt":"2019-09-30T03:23:08","slug":"leetcode-60-permutation-sequence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-60-permutation-sequence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 60. Permutation Sequence"},"content":{"rendered":"\n<p>The set&nbsp;<code>[1,2,3,...,<em>n<\/em>]<\/code>&nbsp;contains a total of&nbsp;<em>n<\/em>! unique permutations.<\/p>\n\n\n\n<p>By listing and labeling all of the permutations in order, we get the following sequence for&nbsp;<em>n<\/em>&nbsp;= 3:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>\"123\"<\/code><\/li><li><code>\"132\"<\/code><\/li><li><code>\"213\"<\/code><\/li><li><code>\"231\"<\/code><\/li><li><code>\"312\"<\/code><\/li><li><code>\"321\"<\/code><\/li><\/ol>\n\n\n\n<p>Given&nbsp;<em>n<\/em>&nbsp;and&nbsp;<em>k<\/em>, return the&nbsp;<em>k<\/em><sup>th<\/sup>&nbsp;permutation sequence.<\/p>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Given&nbsp;<em>n<\/em>&nbsp;will be between 1 and 9 inclusive.<\/li><li>Given&nbsp;<em>k<\/em>&nbsp;will be between 1 and&nbsp;<em>n<\/em>! inclusive.<\/li><\/ul>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 3, k = 3\n<strong>Output:<\/strong> \"213\"\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 4, k = 9\n<strong>Output:<\/strong> \"2314\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string getPermutation(int n, int k) {\n    vector<int> num;\n    vector<int> fact(10, 1);\n    for (int i = 1; i <= 9; i++) {\n      num.push_back(i);\n      fact[i] = fact[i - 1] * i;\n    }\n\n    string s;\n    k--;\n    while (n--) {\n      int d = k \/ fact[n];\n      k %= fact[n];\n      s += ('0' + num[d]);\n      for (int i = d + 1; i <= 9; i++)\n        num[i - 1] = num[i];\n    }\n    return s;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>The set&nbsp;[1,2,3,&#8230;,n]&nbsp;contains a total of&nbsp;n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for&nbsp;n&nbsp;= 3: &#8220;123&#8221;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[501,31,177,121],"class_list":["post-5610","post","type-post","status-publish","format-standard","hentry","category-math","tag-fatorial","tag-math","tag-medium","tag-permutation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5610"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5610\/revisions"}],"predecessor-version":[{"id":5612,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5610\/revisions\/5612"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}