LeetCode Online Judge 题目C# 练习 - Next Permutation
2012-10-02 05:42
405 查看
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
代码分析:
O(n), 其实也是BF而已,主要是找到做的方法。
分三步:
1. 从后往前找falling edge,下降沿。(下降之后的那个元素)
2. 从下降沿开始往后找出替换它的元素。(就是第一个比它小的前一个元素)
3. 反转后面所有元素,让他从小到大sorted(因为之前是从大到小sorted的)
例如 “547532“
1. “547532”, 4是下降沿。
2. “547532”, 5是要替换的元素, 替换后得到 “ 557432”
3. "557432", 7432反转,得到 “552347”。
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
public static void NextPermutation(List<int> num) { if (num.Count <= 1) return; //find the falling edge int edge = -1; for (int i = num.Count - 2; i >= 0; i--) { if (num[i] < num[i + 1]) { edge = i; break; } } if (edge > -1) { //find replacement for (int i = edge + 1; i < num.Count; i++) { if (num[edge] >= num[i]) { NextPermutationSwap(num, edge, i - 1); break; } if (i == num.Count - 1) { NextPermutationSwap(num, edge, i); break; } } } //reverse the following elements for(int i = edge + 1, j = num.Count - 1; i <= edge + (num.Count - edge - 1) / 2; i++, j--) { NextPermutationSwap(num, i, j); } } //swap helper function public static void NextPermutationSwap(List<int> num, int i, int j) { int temp = num[i]; num[i] = num[j]; num[j] = temp; }
代码分析:
O(n), 其实也是BF而已,主要是找到做的方法。
分三步:
1. 从后往前找falling edge,下降沿。(下降之后的那个元素)
2. 从下降沿开始往后找出替换它的元素。(就是第一个比它小的前一个元素)
3. 反转后面所有元素,让他从小到大sorted(因为之前是从大到小sorted的)
例如 “547532“
1. “547532”, 4是下降沿。
2. “547532”, 5是要替换的元素, 替换后得到 “ 557432”
3. "557432", 7432反转,得到 “552347”。
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Wildcard Matching
- LeetCode Online Judge 题目C# 练习 - Longest Palindromic Substring
- LeetCode Online Judge 题目C# 练习 - Minimum Path Sum
- LeetCode Online Judge 题目C# 练习 - N-QueensII
- LeetCode Online Judge 题目C# 练习 - Permutation Sequence
- LeetCode Online Judge 题目C# 练习 - PermutationsII
- LeetCode Online Judge 题目C# 练习 - Reverse Integer
- LeetCode Online Judge 题目C# 练习 - Restore IP Addresses
- LeetCode Online Judge 题目C# 练习 - Text Justification
- LeetCode Online Judge 题目C# 练习 - Largest Rectangle in Histogram
- LeetCode Online Judge 题目C# 练习 - Palindrome Number
- LeetCode Online Judge 题目C# 练习 - Reverse Linked List II
- LeetCode Online Judge 题目C# 练习 - 3SUM
- LeetCode Online Judge 题目C# 练习 - Binary Tree Level Order Traversal
- LeetCode Online Judge 题目C# 练习 - First Missing Positive
- LeetCode Online Judge 题目C# 练习 - Merge Intervals
- LeetCode Online Judge 题目C# 练习 - Rotate List
- LeetCode Online Judge 题目C# 练习 - Search for a Range
- LeetCode Online Judge 题目C# 练习 - Substring with Concatenation of All Words
- LeetCode Online Judge 题目C# 练习 - Generate Parentheses