<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fatorial Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/fatorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/fatorial/</link>
	<description></description>
	<lastBuildDate>Mon, 30 Sep 2019 03:23:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>fatorial Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/fatorial/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 60. Permutation Sequence</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-60-permutation-sequence/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-60-permutation-sequence/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Mon, 30 Sep 2019 03:22:42 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[fatorial]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[permutation]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5610</guid>

					<description><![CDATA[<p>The set&#160;[1,2,3,...,n]&#160;contains a total of&#160;n! unique permutations. By listing and labeling all of the permutations in order, we get the following sequence for&#160;n&#160;= 3: "123"&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-60-permutation-sequence/">花花酱 LeetCode 60. Permutation Sequence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>The set&nbsp;<code>[1,2,3,...,<em>n</em>]</code>&nbsp;contains a total of&nbsp;<em>n</em>! unique permutations.</p>



<p>By listing and labeling all of the permutations in order, we get the following sequence for&nbsp;<em>n</em>&nbsp;= 3:</p>



<ol><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>



<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>



<p><strong>Note:</strong></p>



<ul><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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 3, k = 3
<strong>Output:</strong> "213"
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> n = 4, k = 9
<strong>Output:</strong> "2314"</pre>



<h2><strong>Solution: Math</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(n)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  string getPermutation(int n, int k) {
    vector&lt;int&gt; num;
    vector&lt;int&gt; fact(10, 1);
    for (int i = 1; i &lt;= 9; i++) {
      num.push_back(i);
      fact[i] = fact[i - 1] * i;
    }

    string s;
    k--;
    while (n--) {
      int d = k / fact[n];
      k %= fact[n];
      s += ('0' + num[d]);
      for (int i = d + 1; i &lt;= 9; i++)
        num[i - 1] = num[i];
    }
    return s;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-60-permutation-sequence/">花花酱 LeetCode 60. Permutation Sequence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-60-permutation-sequence/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
