算法的复杂性和效率,指数运行的Java复杂性、算法、效率、指数

2023-09-11 06:08:10 作者:雪花蝶霜

我有一个字符串列表。我有一组数字:

I have a list of strings. I have a set of numbers:

{1,2,3,4}

{1, 2, 3, 4}

和我需要生成所有组合(字符串)要检查我的列表,组合(?):

and I need to generate all combinations(?) (strings) to check against my list, Combinations:

(1,2,3,4),(1234),(1,2,3,4),(123,4),(12,34),(1,2,34),(1, 234),(1,23,4),(1,23),(1,2,3),(1 2),((1 2),(3 4))...等。

(1, 2, 3, 4), (1234), (1, 2, 3, 4), (123, 4), (12, 34), (1, 2, 34), (1, 234), (1, 23, 4), (1, 23), (1, 2, 3), (1 2), ((1 2), (3 4))...etc.

这个问题变大是我的一组数字变大。是不是正确的,这是一个不好的问题,为使用递归? (这就是我现在)但是,是不是迭代求解的空间要求严格,如列表的最大尺寸是多少?

This problem grows larger as my set of numbers gets larger. Is it right that this is a bad problem to use recursion for? (that is what I have now) However, aren't the space requirements stricter for an iterative solution, such as the maximum size of lists?

目前终止,我需要看看匹配的数量,每个结果,我的列表,然后零部件的数量对于每个结果..恩。 (1)= 1; (1,2)= 2

At termination, I need to look at the number of matches, for each result, with my list, and then the number of component parts for each result.. ex. (1) = 1; (1, 2) = 2.

我的电脑是运行内存(这是一个问题与较大的物体抽象)

My computer was running out of memory (this is an abstraction of a problem with larger objects)

编辑:所以我的问题是在一个显著更大的范围内,如图形,在700×500的矩阵比较像素...我的路不可能是最有效的方法来做到这一点..?我需要知道物体的多少$组成它们(这是我的字符串列表)

so my question was in a significantly larger context, such as graphics, comparing pixels in a 700 x 500 matrix... My way cannot be the most efficient way to do this..? I need to know the nested structure of the objects and how many pre-exisiting components that comprise them (that are in my list of strings)

EDIT2:完整的问题描述here.

The full problem is described here.

推荐答案

作为一般规则:当迭代足够不要使用递归

As a general rule: do not use recursion when iteration is sufficient.

递归使用堆栈,而当这种堆栈已满你堆栈溢出。如果你正在执行的东西用阶乘扩展堆栈很少会足够大的递归解决方案是可行的。

Recursion uses a stack, and when that stack is full you have stack overflow. If you're performing something with a factorial expansion the stack will seldom be big enough for a recursive solution to be viable.

如果您可以通过循环或嵌套循环使用实现的东西来代替。

If you can accomplish something by a loop, or a nested loop use that instead.

此外,如果你只是简单检查,对每个东西的组合,你并不需要存储所有组合的结果(这将占用大量的内存),而不是与之比较的每个组合再扔生成的组合了。

Furthermore, if you're simply checking each combination against something, you do not need to store the result of all combinations (which will take up enormous memory), instead compare against each combination then throw that generated combination away.

 
精彩推荐
图片推荐