Skip to content

Commit b516994

Browse files
committed
permutation_sequence
1 parent cfd0f98 commit b516994

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
5252
#### [55. jump game](https://github.com/hitzzc/go-leetcode/tree/master/jump_game)
5353
#### [58. length of last word](https://github.com/hitzzc/go-leetcode/tree/master/length_of_last_word)
5454
#### [59. spiral matrix II](https://github.com/hitzzc/go-leetcode/tree/master/spiral_matrix_II)
55+
#### [60. permutation sequence](https://github.com/hitzzc/go-leetcode/tree/master/permutation_sequence)
5556

5657

5758

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package permutation_sequence
2+
3+
func getPermutation(n int, k int) string {
4+
if n == 0 {
5+
return ""
6+
}
7+
data := make([]int, n)
8+
data[0] = 1
9+
iToS := []byte("123456789")
10+
for i := 1; i < n; i++ {
11+
data[i] = data[i-1] * i
12+
}
13+
ret := ""
14+
k--
15+
for i := n - 1; i >= 0; i-- {
16+
ret += string(iToS[k/data[i]])
17+
iToS = append(iToS[:k/data[i]], iToS[k/data[i]+1:]...)
18+
k = k % data[i]
19+
}
20+
return ret
21+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package permutation_sequence
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetPermutation(t *testing.T) {
8+
println(getPermutation(3, 5))
9+
}

0 commit comments

Comments
 (0)