简体   繁体   English

置换算法内存不足异常

[英]Permutation Algorithm OutOfMemory Exception

Please help me to how to fix OutOfMemory exception using below code: 请帮助我如何使用以下代码修复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;
        }
    }
}

If you run this code using 10 chrs, it throws out of memory exception. 如果使用10 chrs运行此代码,则会抛出内存不足异常。 But for 9 chars , it returns result. 但是对于9个字符,它将返回结果。

It was my interview question to write a code such that it should not go out of memory if big data passed. 我的访谈问题是编写代码,这样如果大数据通过,它就不会耗尽内存。

Thanks, 谢谢,

Your problem is that you want to generate all permutations at once, instead of generating them one-by-one. 您的问题是您想一次生成所有排列,而不是一一生成。 You are looking for an algorithm which produces the next permutation. 您正在寻找可产生下一个排列的算法。

See the first page of Knuth's book on generating permutations. 请参阅Knuth关于生成置换的书的第一页。

I would like to answer my question by myself because it may help someone as well. 我想独自回答我的问题,因为它也可能对某人有所帮助。

I will use Iterator Design Pattern to remove unused elements from memory and will convert collection into sequence. 我将使用迭代器设计模式从内存中删除未使用的元素,并将集合转换为序列。

Thanks, 谢谢,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM