LeetCode Online Judge 题目C# 练习 - Permutation Sequence
2012-10-05 04:39
295 查看
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
代码分析:
如果做了之前的一题, Next Permutation,这一题就是吃菜了。
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
public static string PermutationSequence(int n, int k) { List<int> num = new List<int>(); for (int i = 1; i <= n; i++) { num.Add(i); } for (int i = 1; i < k; i++) { NextPermutation(num); } string ret = ""; foreach (var item in num) { ret += Convert.ToString(item); } return ret; }
代码分析:
如果做了之前的一题, Next Permutation,这一题就是吃菜了。
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Sqrt(x)
- LeetCode Online Judge 题目C# 练习 - Jump Game
- LeetCode Online Judge 题目C# 练习 - Roman to Integer
- LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array
- LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Simplify Path
- LeetCode Online Judge 题目C# 练习 - Multiply Strings
- LeetCode Online Judge 题目C# 练习 - Permutations
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted Array
- LeetCode Online Judge 题目C# 练习 - Scramble String
- LeetCode Online Judge 题目C# 练习 - Unique Binary Search Trees
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted Array II
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted List
- LeetCode Online Judge 题目C# 练习 - Sort Color
- LeetCode Online Judge 题目C# 练习 - Longest Valid Parentheses
- LeetCode Online Judge 题目C# 练习 - Unique Paths II
- LeetCode Online Judge 题目C# 练习 - Merge Two Sorted Lists
- LeetCode Online Judge 题目C# 练习 - Remove Nth Node From End of List
- LeetCode Online Judge 题目C# 练习 - Remove Duplicates from Sorted List II
- LeetCode Online Judge 题目C# 练习 - Rotate Image