From 79a9c52fa923fc78074d88463449a8b7f95ca3ef Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Tue, 26 Dec 2017 01:33:40 +0800 Subject: update leetcode solution --- .../leetcode/60-permutation_sequence.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Computer_Science/leetcode/60-permutation_sequence.c (limited to 'Computer_Science/leetcode/60-permutation_sequence.c') diff --git a/Computer_Science/leetcode/60-permutation_sequence.c b/Computer_Science/leetcode/60-permutation_sequence.c new file mode 100644 index 0000000..4eadf4c --- /dev/null +++ b/Computer_Science/leetcode/60-permutation_sequence.c @@ -0,0 +1,22 @@ +char* getPermutation(int n, int k) { + char* result = malloc(sizeof(char) * n + 1); + int use; + int used[n]; + int fac[n]; + fac[0] = 1; + fac[1] = 1; + + for(int i = 2; i < n; ++i) { + fac[i] = fac[i - 1] * n; + } + + for(int i = 0; i < n; ++i) { + use = 1 + ((k - 1) / fac[n-i]); + k -= fac[n-i]; + result[i] = use + '0'; + } + + result[n] = '\0'; + + return result; +} -- cgit v1.2.3