这不是相当的副本"递归,而不是多回路"但它相当接近.如果这对您没有帮助,我会写一个解决方案.
编辑:这是一个非递归的解决方案.递归的一个稍微难以返回一个IEnumerable
,但返回一个迭代器给了一个很好的接口IMO :)
private static IEnumerableGetAllMatches(char[] chars, int length) { int[] indexes = new int[length]; char[] current = new char[length]; for (int i=0; i < length; i++) { current[i] = chars[0]; } do { yield return new string(current); } while (Increment(indexes, current, chars)); } private static bool Increment(int[] indexes, char[] current, char[] chars) { int position = indexes.Length-1; while (position >= 0) { indexes[position]++; if (indexes[position] < chars.Length) { current[position] = chars[indexes[position]]; return true; } indexes[position] = 0; current[position] = chars[0]; position--; } return false; }