LeetCode——Next Permutation

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

原题链接:https://oj.leetcode.com/problems/next-permutation/

题目:实现 下一个排列,其按字典序重新排列给定数组为比下一个排列更大的排列。

思路:下一个排列实际上比当前排列大且最接近的排列。具体做法是:在数组中从后往前,找到最后升序的地方,交换前后值,并将后面的原序逆转为升序。

	public void nextPermutation(int[] num) {
		int len = num.length, i = 0, j = 0;
		for (i = len - 2; i >= 0; i--) {
			if (num[i] >= num[i + 1])
				continue;
			for (j = len - 1; j > i; j--) {
				if (num[j] > num[i])
					break;
			}
			break;
		}
		if (i >= 0) {
			int temp = num[i];
			num[i] = num[j];
			num[j] = temp;
		}
		int end = len - 1;
		int start = i + 1;
		while (start < end) {
			int temp = num[start];
			num[start] = num[end];
			num[end] = temp;
			start++;
			end--;
		}
	}
时间: 2024-07-28 19:47:32

LeetCode——Next Permutation的相关文章

Leetcode:Next Permutation 下一个排列

Next Permutation: 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 ord

LeetCode OJ--Next Permutation *

求一个排列的下一个排列. 1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1 #include <iostream> #include <vector> #include <algorithm> using namespace std; class Solution{ public: void nextPermutation(vector<int> &num) { if(num.size() == 0) return; c

【leetcode】 Permutation Sequence

问题: 对于给定序列1...n,permutations共有 n!个,那么任意给定k,返回第k个permutation.0 < n < 10. 分析: 这个问题要是从最小开始直接到k,估计会超时,受10进制转换为二进制的启发,对于排列,比如 1,2,3 是第一个,那么3!= 6,所以第6个就是3,2,1.也就是说,从开始的最小的序列开始,到最大的序列,就是序列个数的阶乘数.那么在1,3 , 2的时候呢?调整一下,变成2,1,3,就可以继续. 实现: int getFactorial(int n

LeetCode: Next Permutation [030]

[题目] 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 repl

Leetcode Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome. For example,"code" -> False, "aab" -> True, "carerac" -> True. Hint: Consider the palindromes of odd vs even length. What difference d

[LeetCode] Find Permutation 找全排列

By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decreasing relationship between two numbers, 'I' represents an increasing relationship between two numbers. And our secret signature was constructed by a s

[leetcode]Next Permutation @ Python

原题地址:https://oj.leetcode.com/problems/next-permutation/ 题意: 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 p

LeetCode Next Permutation

原题链接在这里:https://leetcode.com/problems/next-permutation/ 参考了这篇博文:http://blog.csdn.net/linhuanmars/article/details/20434115 分两种情况: 1. 从前往后一路数值一直变小,如"654321", 返回的应该是"123456"也就是整个反转 2. 数值没有一直变下,如"236541", 此时从后往前找到第一个数值下降的点3<6,

Leetcode 60. Permutation Sequence

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): Given n and k, return the kth permutation sequence. Note: Given n will be between