diff options
author | crupest <crupest@outlook.com> | 2020-05-15 21:32:55 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-05-15 21:32:55 +0800 |
commit | 8297cde550c95061718e38a0eaf299edd17f0470 (patch) | |
tree | 0ef35b0ee7c1ffddcb01ad7dd86362a801edf9b4 /works/solutions/cpp | |
parent | 60d034ae1d9c699fcdb62918ccf820d662da4f3e (diff) | |
download | crupest-8297cde550c95061718e38a0eaf299edd17f0470.tar.gz crupest-8297cde550c95061718e38a0eaf299edd17f0470.tar.bz2 crupest-8297cde550c95061718e38a0eaf299edd17f0470.zip |
import(solutions): Add problem 60 .
Diffstat (limited to 'works/solutions/cpp')
-rw-r--r-- | works/solutions/cpp/60.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/works/solutions/cpp/60.cpp b/works/solutions/cpp/60.cpp new file mode 100644 index 0000000..f090355 --- /dev/null +++ b/works/solutions/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 |