summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-05-15 21:32:55 +0800
committercrupest <crupest@outlook.com>2020-05-15 21:32:55 +0800
commit38576346da544bd44fc58b65a0a17d9a0cf80130 (patch)
tree06b52072b2498414beb53fdfa677282a712f84c7
parent65260343ad6b769c9a57eac206ddd77a57f81116 (diff)
downloadsolutions-38576346da544bd44fc58b65a0a17d9a0cf80130.tar.gz
solutions-38576346da544bd44fc58b65a0a17d9a0cf80130.tar.bz2
solutions-38576346da544bd44fc58b65a0a17d9a0cf80130.zip
Add problem 60 .
-rw-r--r--cpp/60.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/cpp/60.cpp b/cpp/60.cpp
new file mode 100644
index 0000000..f090355
--- /dev/null
+++ b/cpp/60.cpp
@@ -0,0 +1,44 @@
+#include <string>
+#include <vector>
+
+using std::string;
+
+class Solution
+{
+public:
+ string getPermutation(int n, int k)
+ {
+ k--;
+
+ string result;
+ result.reserve(n);
+
+ std::vector<char> nums;
+ nums.reserve(n);
+
+ for (int i = 1; i <= n; i++)
+ {
+ nums.push_back(i + '0');
+ }
+
+ n--;
+ int fac = 1;
+ for (int i = 2; i <= n; i++)
+ {
+ fac *= i;
+ }
+
+ while (n > 0)
+ {
+ const int index = k / fac;
+ k = k % fac;
+ result += nums[index];
+ nums.erase(nums.cbegin() + index);
+ fac /= n--;
+ }
+
+ result += nums.front();
+
+ return result;
+ }
+}; \ No newline at end of file