Study Notes: arrange and combine

Source: Internet
Author: User

Use string [] arr = {"qi", "ji", "ta"}; // sort the List <string []> lst_Permutation = PermutationAndCombination <string>. getPermutation (arr, 2); // returns the combined List <string []> lst_Combination = PermutationAndCombination <string>. getCombination (arr, 2); Code

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApplication1 {public class PermutationAndCombination <T> {// <summary> // exchange two variables /// </summary> // <param name = "a"> variable 1 </param> /// <param name = "B"> variable 2 </param> public static void Swap (ref T, ref T B) {T temp = a; a = B; B = temp;} // <summary> // recursive algorithm to find the combination of arrays (Private Members) /// </summary> /// <param name = "list"> returned Model </param> /// <param name = "t"> array </ param> /// <param name = "n"> auxiliary variable </param> /// <param name = "m"> auxiliary variable </param> // <param name = "B"> secondary array </param> // <param name = "M"> secondary variable M </param> private static void GetCombination (ref List <T []> list, T [] t, int n, int m, int [] B, int M) {for (int I = n; I> = m; I --) {B [m-1] = I-1; if (m> 1) {GetCombination (ref list, t, I-1, m-1, B, M );} else {if (list = null) {list = new List <T []> ();} T [] temp = new T [M]; for (int j = 0; j <B. length; j ++) {temp [j] = t [B [j];} list. add (temp) ;}}/// <summary> // recursive algorithm for sorting (Private member) /// </summary> /// <param name = "list"> returned list </param> /// <param name = "t"> array </param> /// <param name = "startIndex"> start label </param> /// <param name = "endIndex"> end label </param> private static void GetPermutation (ref List <T []> list, T [] t, int startIndex, int endIndex) {if (startIndex = endIndex) {if (list = null) {list = new List <T []> ();} T [] temp = new T [t. length]; t. copyTo (temp, 0); list. add (temp);} else {for (int I = startIndex; I <= endIndex; I ++) {Swap (ref t [startIndex], ref t [I]); getPermutation (ref list, t, startIndex + 1, endIndex); Swap (ref t [startIndex], ref t [I]) ;}} /// <summary> /// sort the start and end labels, other elements remain unchanged /// </summary> /// <param name = "t"> array </param> /// <param name = "startIndex"> Start number </param> /// <param name = "endIndex"> end label </param> /// <returns> type from start label to end label </returns> public static List <T []> GetPermutation (T [] t, int startIndex, int endIndex) {if (startIndex <0 | endIndex> t. length-1) {return null;} List <T []> list = new List <T []> (); GetPermutation (ref list, t, startIndex, endIndex ); return list ;} /// <summary> /// return the full arrangement of all elements in the array /// </summary> /// <param name = "t"> array </param> /// <returns> fully-arranged paradigm </returns> public static List <T []> GetPermutation (T [] t) {return GetPermutation (t, 0, t. length-1 );} /// <summary> /// sort the n elements in the array /// </summary> /// <param name = "t"> array </param> /// <param name = "n"> Number of elements </param> /// arrange n elements in the <returns> array </returns> public static List <T []> GetPermutation (T [] t, int n) {if (n> t. length) {return null;} List <T []> list = new List <T []> (); List <T []> c = GetCombination (t, n ); for (int I = 0; I <c. count; I ++) {List <T []> l = new List <T []> (); GetPermutation (ref l, c [I], 0, n-1); list. addRange (l);} return list ;} /// <summary> /// evaluate the combination of n elements in the array /// </summary> /// <param name = "t"> obtain the array </param> /// <param name = "n"> Number of elements </param> /// the combination of n elements in the <returns> array. </returns> public static list <T []> GetCombination (T [] t, int n) {if (t. length <n) {return null;} int [] temp = new int [n]; List <T []> list = new List <T []> (); getCombination (ref list, t, t. length, n, temp, n); return list ;}}}

 


Related Article

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.