LeetCode_45permute [Permutations]

#pragma warning(disable:4996)

#include <cstdio>
#include <tchar.h>
#include <Windows.h>
#include <vector>
using namespace std;

/*
	submit time : 1
	request :
		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].
*/

void Swap(int* data, int i, int j) {
	int temp = data[i];
	data[i] = data[j];
	data[j] = temp;
}

void helpPermute(vector<vector<int> >& result, vector<int>& num, int length, int index) {
	if (index == length - 1)
		result.push_back(num);

	int vernier = index;
	while (vernier < length) {
		Swap(&num[0], index, vernier);
		helpPermute(result, num, length, index + 1);
		Swap(&num[0], index, vernier);
		++vernier;
	}
}

vector<vector<int> > permute(vector<int> &num) {
	vector<vector<int> > result;
	int length = num.size();
	if (length == 0) return result;

	helpPermute(result, num, length, 0);

	return result;
}

//===================Test====================
void printVec(vector<int>& vec) {
	vector<int>::iterator iter = vec.begin();
	for (; iter != vec.end(); ++iter)
		printf("%d ", *iter);
	printf("\n");
}

void Test(vector<int>& num) {
	vector<vector<int> > result = permute(num);
	vector<vector<int> >::iterator iter = result.begin();
	for (; iter != result.end(); ++iter)
		printVec(*iter);

	printf("\n");
}

void Test1() {
	int data[] = { 3, 7, 9, 13 };
	vector<int> num(data, data + sizeof(data) / sizeof(int));
	Test(num);
}

int _tmain(int arc, _TCHAR* argv[]) {
	Test1();

	system("pause");
	return 0;
}

LeetCode_45permute [Permutations],布布扣,bubuko.com

时间: 2024-08-24 05:16:28

LeetCode_45permute [Permutations]的相关文章

Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] 分析: 全组合的思想,保证start和end之间交换的时候中间没有与end相同的数字 class Solution

LeetCode46,47 Permutations, Permutations II

题目: LeetCode46 I Given a collection of distinct numbers, return all possible permutations. (Medium) For example,[1,2,3] have the following permutations: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] LeetCode47 II Given a collection of numb

HDOJ 5338 ZZX and Permutations 线段树+树状数组

[题意]: 给一个排列加上表示循环的括号,问如何让1到n的对应的字典序最大. 从1开始贪心每个数字可以往三个地方走,右边第一个,跳转到左边的某一个,和自己构成循环 对于走到右边第一个的情况,只要判断右边的那个有没有被占据就可以了,如果可以和右边的互换,那么需要在线段树中将右边的数置为0 跳转到左边的某一个,一个数如果跳转到左边的某一个则说明左边的那个是括号开头这个数是括号结尾,用一个线段树查询区间里的最大值,由于括号间是不能重叠的,所以需要用树状数组二分一下左边界.如果这个数是要跳转到左边的某个

[LeetCode] Permutations

Well, have you solved the nextPermutation problem? If so, your code can be used in this problem. The idea is fairly simple: sort nums in ascending order, add it to res; generate the next permutation of nums using nextPermutation(), and add it to res;

[LintCode] Permutations

http://www.lintcode.com/en/problem/permutations/# Given a list of numbers, return all possible permutations. Example For nums = [1,2,3], the permutations are: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1] ] class Solution { public: /** * @pa

UVA 11077 Find the Permutations DP

Find the Permutations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem D Find the Permutations Input: Standard Input Output: Standard Output Sorting is one of the most used operations in real lif

Leetcode: Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 难度:80+15(怎样跳过重复的case来节省时间).我一开始的想法很简单,就是在permutation这道题的

poj 2369 Permutations 置换水题

找循环节求lcm就够了,若答案是12345应该输出1,被坑了下. #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<algorithm> #include<cmath> using namespace std; #define INF 0x3FFFFFF #define MAXN 2222 #define eps 1e-6 i

LeetCode: Permutations [045]

[题目] 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]. [题意] 给定一个数组,生成所有的全排列 [思路] 递归,类DFS [代码] class Solution { public: void