下面的算法,并给出我们应该把它写在Java中。然而,当我试图理解一行行,它变得混乱,特别是部分:
The following algorithm is given and we are supposed to write it out in java. However, when I try to understand line by line, it gets confusing, especially the part:
A [k + 1:N-1] =升序排列S中的值
A[k+1:N-1] = the values in S in ascending order
要我明白了,只设置有1个号码在任何时候。我们如何能取代 A [k + 1:N-1]
的设置时,只有1号
To my understand, the set only have 1 number at anytime. How can we replace A[k+1:N-1]
when the set only has 1 number?
设A是按升序排列(假设它是整数数组 0
到 N-1
序列对 INT [N]
)。
Let A be a sequence of integers 0
to N-1
in ascending order (let's assume its an array of int[N]
).
next_permutation(A):
k = N-1
S = { }
while k >= 0:
if S contains a value larger than A[k]:
v = the smallest member of S that is larger than A[k]
remove v from S
insert A[k] in S
A[k] = v
A[k+1:N-1] = the values in S in ascending order.
return true
else:
insert A[k] in S
k -= 1
return false
所示的算法是类似于生成字典顺序。你可以阅读这篇文章以获取更多信息。
The algorithm shown is similar to the Generation in lexicographic order. You can read the article for further information.
@templatetypedef如何我可以值一组按升序排列替换数组中的项目任何线索?例如A [k + 1:N-1] =升序排列S中的值。我应该使用的toArray()?
@templatetypedef Any clue on how I can replace items in an array by values in a set in ascending order? e.g. A[k+1:N-1] = the values in S in ascending order. Should I use toArray()?
这是没有必要的。你可以尽量保持S数组排序的所有的时间。要插入阵列中的一个新的号码时,都将其插入这样的,所以阵列能留下来分类的。
例如,如果你有 S = [5 7 8]
到目前为止,你要插入 6
,你插5至7 - S = [5 6 7 8]
。以此方式,替换步骤选自S的元素仅仅复制到A [K + 1:N-1]
This is not needed. You can try to keep the S array sorted all the time. Every time you want to insert a new number in the array, you insert it in such, so the array could stay sorted.
For example, if you have S = [5 7 8]
so far and you want to insert 6
, you inserted between 5 and 7 - S = [5 6 7 8]
. This way the replacement step is just copying the elements from S to A[k+1:N-1].