2017 Little Red Book Campus recruitment interview Experience

Source: Internet
Author: User
Tags bool int size

Little Red Book is a Shanghai-based e-commerce, although not the reputation of Alibaba but also is a fast-growing enterprise, the company attaches importance to the algorithm, the need to use a recursive method to write

A full arrangement, fortunately still remember some ideas, and finally I wrote out, and then wrote the code:

#include <iostream> #include <vector> #include <map> #include <set> #include <sstream> #


include<fstream> #include <cstring> #include <stack> #include <algorithm> using namespace std; Vector<vector<int>>get_all_permutation (vector<int>&data, int end) {Vector<vector<int
	>>re;
	vector<vector<int>>tmp;
		if (end = = 0) {vector<int>temp;
		Temp.push_back (Data[end]);
		Re.push_back (temp);
	return re;
	} re = get_all_permutation (data, end-1);
		for (int i = 0; I<re.size (); i++) {int count = 0;
		Vector<int>::iterator ITER;
		int size = Re[i].size ();
		int index = 0;
			while (count! = size + 1) {iter = Re[i].begin ();
			for (int k = 0; k < index; k++) iter++;
			Re[i].insert (ITER, data[end]);
			Tmp.push_back (Re[i]);
			iter = Re[i].begin ();
			for (int k = 0; k < index; k++) iter++;
			Re[i].erase (ITER);
			index++;
		count++;
}} return tmp;
	} int main () {int num;vector<int>data;
	vector<vector<int>>re;
	int count = 1;
		while (count) {num = 0;
			if (num<1) {cout << 0 << Endl;
		return 0;
		} for (int i = 1; I <= num; i++) data.push_back (i);
		Re = get_all_permutation (data, num-1);
		cout << ' [';
			for (int i = 0; I<re.size (); i++) {cout << ' [';
				for (int j = 0; J<re[i].size (); j + +) {cout << re[i][j];
			if (J! = Re[i].size ()-1) cout << ', ';
			} cout << ';
		if (i! = Re.size ()-1) cout << ', ';
		} cout << '] ' << Endl;
	count--;
} return 0; 
}//input: Number n//output: The full array of [,..., N] is output in the form of two-dimensional arrays.
 For example: when n=3, output [[+/-], [1,3,2], ..., [3,2,1]]//Standard solution: recursive.


This method is more chaotic, the main idea is to use the intervening spaces method, assuming we in the case of a,b,c three characters, we call function recursive call return b,c and c,b then we can insert a into b,c and c,b each bit

, then for B,c, there will be ABC,BAC,BCA, for c,b, we have ACB,CAB,CBA;

But under normal circumstances, our output sequence is:

1, 2, 3
1, 3, 2
2, 1, 3
2, 3, 1
3, 1, 2
3, 2, 1

Then another method was written:

#include <iostream> #include <string> #include <vector> #include <memory> #include <fstream

> Using namespace std; Vector<vector<int>> get_all_permutation (Vector<int>&data,int begin) {vector<vector<
	int>>re;
	vector<int>tmp;
		if (begin = = Data.size ()-2) {//This time there are two elements left Tmp.push_back (Data[begin]);
		Tmp.push_back (data[begin+1]);
		Re.push_back (TMP);
		Exchange two numbers tmp[0] = tmp[0] ^ tmp[1];
		TMP[1] = tmp[0] ^ tmp[1];
		Tmp[0] = tmp[0] ^ tmp[1];
		Re.push_back (TMP);
	return re;
	} re = get_all_permutation (data,begin+1);
	for (int i = 0; i < re.size (); i++) Re[i].insert (Re[i].begin (), Data[begin]);
	int num = Re.size ();
			for (int i = 0; i < num; i++) {//exchange the No. 0 subscript and the element starting from the 1th index for the for (int j = 1; J < Re[i].size (), j + +) {tmp = Re[i];
			Tmp[0] = tmp[0] + tmp[j];
			TMP[J] = tmp[0]-tmp[j];
			Tmp[0] = tmp[0]-tmp[j];
		Re.push_back (TMP);
}} return re;
	} int main () {int num;
	vector<int>data; Vector<vector<int>>re;
		while (Cin>>num) {if (num = = 0) return 0;
		if (num = = 1) {cout << num << endl; continue;}
		for (int i = 1; I <= num; i++) data.push_back (i);
		Re = get_all_permutation (data,0);
		cout << ' [';
			for (int i = 0; i < re.size (); i++) {cout << ' [';
				for (int j = 0; J < Re[i].size (); j + +) {cout << re[i][j];
			if (J! = Re[i].size ()-1) cout << ', ';
		} cout << ';
		} cout << '] ' << Endl;
	Data.clear (); }
}

Then use the structure of the tree to write it again (this idea uses DFS and backtracking methods):

#include <vector> #include <memory> #include <fstream> #include <stack> using namespace std; void Get_all_permutation (VECTOR&LT;INT&GT;&AMP;DATA,VECTOR&LT;BOOL&GT;&AMP;FLAG,VECTOR&LT;INT&GT;&AMP;STK) {//
	Begin represents the current subscript int size = Data.size ();
		if (stk.size () = = Data.size ()) {cout << ' [';
			for (int i = 0; i < stk.size (); i++) {cout << stk[i];
		if (i! = Stk.size ()-1) cout << ', ';
		} cout << ';
	Return
			} for (int i = 0; i < size;i++) {if (Flag[i]! = True) {//can continue to access flag[i] = true;
			Stk.push_back (Data[i]);
			Get_all_permutation (DATA,FLAG,STK);
			Stk.pop_back ();
		Flag[i] = false;
	}}} int main () {int num;
	vector<int>data;
	vector<vector<int>>re;
	vector<bool>flag;
		while (Cin>>num) {if (num = = 0) return 0;
		if (num = = 1) {cout << num << endl; continue;}
		for (int i = 0; i < num; i++) Flag.push_back (false);
	for (int i = 1; I <= num; i++) data.push_back (i);	cout << ' [';
		int count = 0;
		vector<int>stack;
		Get_all_permutation (Data,flag,stack);
		cout << '] ' << Endl;
		Data.clear ();
	Flag.clear (); }
}

Can be implemented with the Next_permutation function in STL



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.