Permutation problems

Reference:http://blog.csdn.net/lbyxiafei/article/details/9328091 

3. Permutation I 

4. Permutation II (No duplicated results)

原题:

Given a collection of numbers, return all possible permutations.

For example,

[1,2,3] have the following permutations:

[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2], and[3,2,1].


代码:

public class Solution {
	public ArrayList<ArrayList<Integer>> permute(int[] num) {
		// IMPORTANT: Please reset any member data you declared, as
		// the same Solution instance will be reused for each test case.
		Arrays.sort(num); // Only for Permutation II
		ArrayList<ArrayList<Integer>> results = new ArrayList<ArrayList<Integer>>();
		ArrayList<Integer> result = new ArrayList<Integer>();
		// record whether this char is available to use
		boolean[] visited = new boolean[num.length]; 
		permute(num, results, visited, result);
		return results;
	}

	// DFS
	private void permute(int[] num, ArrayList<ArrayList<Integer>> results,
			boolean[] visited, ArrayList<Integer> result) {
		if (result.size() == num.length) {
			results.add(new ArrayList<Integer>(result));
			return;
		}
		for (int i = 0; i < num.length; i++) { // for loop makes recursive simpler
			if (visited[i])
				continue;
			result.add(num[i]);
			visited[i] = true;
			permute(num, results, visited, result); // add new char to the result
			visited[i] = false;
			// result.remove(num[i]);
			result.remove(result.size() - 1); // remove the last element

			while (i + 1 < num.length && num[i + 1] == num[i])
				i++; // skip duplicated elements, For Permutation II
		}
	}
}

1. NextPermutation

原题如下:

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,31,3,2

3,2,11,2,3

1,1,51,5,1


方法:

1. 找到最右边的最长降序字段,逆序之

2. 指针一指向该字段的左一节点,指针二指向指针一的右边一个节点(即降序字段逆序后的左一节点)

3. 如果指针一不指向数组头节点,指针二向右移动,直到遇到一个大于指针一的值的节点,两个指针交换

4. 如果指针一指向头结点,直接返回


代码:

public class Solution {
	public void nextPermutation(int[] num) {
		// 找到最右边的递减序列
		int cur = num.length - 1;
		while (cur > 0 && num[cur - 1] >= num[cur])
			cur--;

		reverse(num, cur, num.length - 1);

		int next = cur;
		cur--;

		// next:最右递减序列的第一个元素,current:最右递减序列的左边元素
		// exp: 13,542 --> 13,245: num[next] = num[2] = 2, current = 1
		// exp:,54321 --> ,12345: num[next] = num[0] = 1, current = -1
		// current < 0 说明最开始是一个递减序列,即最后一个permutation, 直接返回其reversed的数组即可
		while (next < num.length) {
			// 找到第一个比current大的next值, swap, 即可找到next permutation
			// exp: 13,245: current = 1, num[current] = 3,
			// 第一个比3大的是num[next=3]=4, swap得14,235,返回
			if (cur >= 0 && num[next] > num[cur]) {
				swap(num, cur, next);
				break;
			}
			next++; // current不动,后移next
		}
	}

	private void swap(int[] num, int i, int j) {
		int temp = num[i];
		num[i] = num[j];
		num[j] = temp;
	}

	private void reverse(int[] num, int i, int j) {
		while (i < j)
			swap(num, i++, j--);
	}
}

2. 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):

  1. "123"

  2. "132"

  3. "213"

  4. "231"

  5. "312"

  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

代码:



你可能感兴趣的:(LeetCode)