Skip to content

Commit 09e5f3e

Browse files
add 60
1 parent 1918f1c commit 09e5f3e

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ LeetCode
5959
|0056|[Merge Intervals](https://leetcode.com/problems/merge-intervals/) | c | [c++](./src/0056-Merge-Intervals/0055.cpp) |[python](./src/0056-Merge-Intervals/0055.py)|||Medium|
6060
|0058|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/) | c | [c++](./src/0058-Length-of-Last-Word/0058.cpp) |[python](./src/0058-Length-of-Last-Word/0058.py)|||Easy|
6161
|0059|[Spiral Matrix II](https://leetcode.com/problem/spiral-matrix-ii/) | c | [c++](./src/0059-Spiral-Matrix-II/0059.cpp) |[python](./src/0059-Spiral-Matrix-II/0059.py)|||Hard|
62+
|0060|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/) | c | [c++](./src/0060-Permutation-Sequence/0060.cpp) |[python](./src/0060-Permutation-Sequence/0060.py)|||Medium|
6263
|0061|[Rotate List](https://leetcode.com/problems/rotate-list/) | c | [c++](./src/0061-Rotate-List/0061.cpp) |[python](./src/0061-Rotate-List/0061.py)|||Medium|
6364
|0062|[Unique Paths](https://leetcode.com/problems/unique-paths/) | c | [c++](./src/0062-Unique-Paths/0062.cpp) |[python](./src/0062-Unique-Paths/0062.py)|||Medium|
6465
|0063|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/) | c | [c++](./src/0063-Unique-Paths-II/0063.cpp) |[python](./src/0063-Unique-Paths-II/0063.py)|||Medium|
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }();
2+
class Solution
3+
{
4+
public:
5+
string getPermutation(int n, int k)
6+
{
7+
string res;
8+
int fac = 1;
9+
for (int i = 1; i <= n; ++i)
10+
{
11+
res.push_back('0' + i);
12+
fac *= i;
13+
}
14+
--k;
15+
for (auto first = res.begin(); n > 0; --n, ++first)
16+
{
17+
fac /= n;
18+
auto index = first + k / fac;
19+
rotate(first, index, index+1);
20+
k %= fac;
21+
}
22+
return res;
23+
}
24+
};

src/0060-Permutation-Sequence/0060.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution:
2+
def getPermutation(self, n, k):
3+
"""
4+
:type n: int
5+
:type k: int
6+
:rtype: str
7+
"""
8+
factorials = [1]*(n+1)
9+
for i in range(1, n+1):
10+
factorials[i] = factorials[i-1]*i
11+
12+
n_list = [i for i in range(1, n+1)]
13+
return self.helper(n, k-1, n_list, factorials)
14+
15+
def helper(self, n, k, n_list, factorials):
16+
if n == 1:
17+
return str(n_list[0])
18+
19+
m = k // factorials[n-1]
20+
k %= factorials[n-1]
21+
res = str(n_list[m])
22+
n_list.remove(n_list[m])
23+
res += self.helper(n-1, k, n_list, factorials)
24+
return res
25+
26+
if __name__ == "__main__":
27+
n, k = 4, 14
28+
print(Solution().getPermutation(n, k))

0 commit comments

Comments
 (0)