簡體   English   中英

置換算法內存不足異常

[英]Permutation Algorithm OutOfMemory Exception

請幫助我如何使用以下代碼修復OutOfMemory異常:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Permutation
{
    class Program
    {
        static void Main(string[] args)
        {
            CreatePartsByFreezingEachElementOnce("abcedfghijk");

            PrintPossiblePermutations(false);
            Console.WriteLine("---------------");

            PrintPossiblePermutations(true);
            Console.ReadLine();
        }

        static void PrintPossiblePermutations(bool unique)
        {
            var allPermutations = new List<string>();
            foreach (var item in _listWithFreezedKey)

                if (item.Item2.Count() == 2)
                {
                    allPermutations.Add(Swap(String.Join(",", item.Item1), item.Item2[0], item.Item2[1]));
                    allPermutations.Add(Swap(String.Join(",", item.Item1), item.Item2[1], item.Item2[0]));
                }

            if (unique)
            {
                var uniuePermutations = allPermutations.Distinct();
                //   PrintPermutations(uniuePermutations.ToList());
                Console.WriteLine(uniuePermutations.Count());
            }
            else
            {
                //    PrintPermutations(allPermutations);
                Console.WriteLine(allPermutations.Count());
            }

        }

        static void PrintPermutations(List<string> permutations)
        {
            int i = 1;
            foreach (var item in permutations)
            {
                Console.WriteLine(string.Format("{0}    :{1}", i, item));
                i++;
            }
        }

        static List<Tuple<List<char>, List<char>>> _listWithFreezedKey = new List<Tuple<List<char>, List<char>>>();
      static void CreatePartsByFreezingEachElementOnce(string str, List<char> indexToFreeze = null)
                    {
                        List<Tuple<List<char>, List<char>>> _innerlistWithFreezedKey = new List<Tuple<List<char>, List<char>>>();


                        var arr = str.ToCharArray().ToList();
                        var copy = arr;
                        if (indexToFreeze == null)
                        {
                            indexToFreeze = new List<char>();
                        }
                        for (int i = 0; i < arr.Count(); i++)
                        {
                            copy = str.ToCharArray().ToList();
                            var nth = arr[i];
                            copy.RemoveAt(i);

                            indexToFreeze.Add(nth);

                            _listWithFreezedKey.Add(new Tuple<List<char>, List<char>>(indexToFreeze.ToList(), copy));
                            _innerlistWithFreezedKey.Add(new Tuple<List<char>, List<char>>(indexToFreeze.ToList(), copy));

                            indexToFreeze.RemoveAt(indexToFreeze.Count() - 1);
                        }

                        foreach (var item in _innerlistWithFreezedKey)
                        {                              
                                List<char> l = item.Item2;
                                CreatePartsByFreezingEachElementOnce(String.Join("", l), item.Item1);
                         }


                        }

        static string Swap(string frezedPart, char swapChar1, char swapChar2)
        {
            return frezedPart + "," + swapChar1 + "," + swapChar2;
        }
    }
}

如果使用10 chrs運行此代碼,則會拋出內存不足異常。 但是對於9個字符,它將返回結果。

我的訪談問題是編寫代碼,這樣如果大數據通過,它就不會耗盡內存。

謝謝,

您的問題是您想一次生成所有排列,而不是一一生成。 您正在尋找可產生下一個排列的算法。

請參閱Knuth關於生成置換的書的第一頁。

我想獨自回答我的問題,因為它也可能對某人有所幫助。

我將使用迭代器設計模式從內存中刪除未使用的元素,並將集合轉換為序列。

謝謝,

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM