Skip to content

Commit 823d01d

Browse files
[N-0] refactor 60
1 parent a3fe473 commit 823d01d

File tree

2 files changed

+24
-20
lines changed

2 files changed

+24
-20
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ Your ideas/fixes/algorithms are more than welcome!
601601
|63|[Unique Paths II](https://leetcode.com/problems/unique-paths-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_63.java)|O(m*n)|O(m*n)|Medium| DP
602602
|62|[Unique Paths](https://leetcode.com/problems/unique-paths/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_62.java)|O(m*n)|O(m*n)|Medium| DP
603603
|61|[Rotate List](https://leetcode.com/problems/rotate-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_61.java)|O(n)|O(1)|Medium| Linked List
604-
|60|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_60.java)|?|?|Medium|
604+
|60|[Permutation Sequence](https://leetcode.com/problems/permutation-sequence/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_60.java)|O(n^2)|O(n)|Medium| Math, Backtracking
605605
|59|[Spiral Matrix II](https://leetcode.com/problems/spiral-matrix-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_59.java)|O(n)|O(n)|Medium|
606606
|58|[Length of Last Word](https://leetcode.com/problems/length-of-last-word/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_58.java)|O(n)|O(1)|Easy|
607607
|57|[Insert Intervals](https://leetcode.com/problems/insert-interval/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_57.java)|O(n)|O(1)|Hard| Array, Sort
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 60. Permutation Sequence
5+
*
46
* The set [1,2,3,…,n] contains a total of n! unique permutations.
57
68
By listing and labeling all of the permutations in order,
@@ -18,27 +20,29 @@ We get the following sequence (ie, for n = 3):
1820
*/
1921
public class _60 {
2022

21-
public String getPermutation(int n, int k) {
22-
int[] nums = new int[n + 1];
23-
int permcount = 1;
24-
for (int i = 0; i < n; i++) {
25-
nums[i] = i + 1; // put 1, 2, 3 ... n into nums[]
26-
permcount *= (i + 1);
27-
}
23+
public static class Solution1 {
24+
public String getPermutation(int n, int k) {
25+
int[] nums = new int[n + 1];
26+
int permcount = 1;
27+
for (int i = 0; i < n; i++) {
28+
nums[i] = i + 1; // put 1, 2, 3 ... n into nums[]
29+
permcount *= (i + 1);
30+
}
2831

29-
k--;
30-
StringBuilder sb = new StringBuilder();
31-
for (int i = 0; i < n; i++) {
32-
permcount = permcount / (n - i);
33-
int idx = k / permcount;// the index that this position should
34-
// choose
35-
sb.append(nums[idx]);
36-
// left shift nums[] by one bit
37-
for (int j = idx; j < n - i; j++) {
38-
nums[j] = nums[j + 1];
32+
k--;
33+
StringBuilder sb = new StringBuilder();
34+
for (int i = 0; i < n; i++) {
35+
permcount = permcount / (n - i);
36+
int idx = k / permcount;// the index that this position should
37+
// choose
38+
sb.append(nums[idx]);
39+
// left shift nums[] by one bit
40+
for (int j = idx; j < n - i; j++) {
41+
nums[j] = nums[j + 1];
42+
}
43+
k %= permcount;
3944
}
40-
k %= permcount;
45+
return sb.toString();
4146
}
42-
return sb.toString();
4347
}
4448
}

0 commit comments

Comments
 (0)