我需要将数组按数字顺序组合成一个数组,问题是当其中一个数组在数组中将两个数组组合在一起时,iA或iB大于数组长度
static void Merge(int[] a, int[] b)
{
int[] c = new int[a.Length + b.Length];
int iA = 0, iB = 0, iC = 0;
while (iA < a.Length && iB < b.Length)
{
if (a[iA] < b[iB])
{
c[iC] = a[iA];
iC++; iA++;
}
else if (a[iA] > b[iB])
{
c[iC] = b[iB];
iC++; iB++;
}
else
{
c[iC] = a[iA];
iC++; iA++;
c[iC] = b[iB];
iC++; iB++;
}
}
for (int i = 0; i < c.Length; i++)
{
Console.WriteLine(c[i]);
}
}然后我执行它
static void Main()
{
int[] a = { 1, 2, 3, 4, 5};
int[] b = { 2, 5, 8, 11};
Merge(a, b);
}这是输出
5
8
10
11
0
0
0
0
Press any key to continue . . .拜托,我需要找到一个解决办法
发布于 2021-01-09 21:06:10
我们所需要的只是for循环:
static void Merge(int[] left, int[] right)
for (int indexLeft = 0, indexRight = 0;
indexLeft < left.Length || indexRight < right.Length;)
if (indexRight >= right.Length ||
(indexLeft < left.Length && left[indexLeft] <= right[indexRight]))
Console.WriteLine(left[indexLeft++]);
else
Console.WriteLine(right[indexRight++]);
}我们移动left和right数组(indexLeft,indexRight),并从left数组打印项当且仅当
我们耗尽了indexRight >= right.Length.
indexRight >= right.Length.
left和right数组以及left[indexLeft] <= right[indexRight]中都有项
否则,我们打印right数组项。
编辑:如果要返回合并数组(result),则为:
static int[] Merge(int[] left, int[] right) {
int[] result = new int[left.Length + right.Length];
int index = 0;
for (int indexLeft = 0, indexRight = 0;
indexLeft < left.Length || indexRight < right.Length;)
if (indexRight >= right.Length ||
(indexLeft < left.Length && left[indexLeft] <= right[indexRight]))
result[index++] = left[indexLeft++];
else
result[index++] = right[indexRight++];
return result;
}发布于 2021-01-09 21:03:19
这里有一个完全不同的方法来解决这个问题。它与实现IComparable<T>的任何类型的集合(即任何IEnumerable<T> where T: IComparable<T> )一起工作。
它使用简单的枚举,没有索引。它确实要求输入是预先排序的。我没有测试如果预条件是假的会发生什么(我也没有检查预条件(因为您不想排序))。
public static IEnumerable<T> Merge<T>(IEnumerable<T> first, IEnumerable<T> second) where T : IComparable<T>
{
//assumption: both first and second are already sorted.
var firstEnumerator = first.GetEnumerator();
var secondEnumerator = second.GetEnumerator();
var result = new List<T>();
if (!firstEnumerator.MoveNext())
{
//nothing in first collection, so...
return second;
}
if (!secondEnumerator.MoveNext())
{
//nothing in second collection, so...
return first;
}
var firstComplete = false;
var secondComplete = false;
while (true)
{
if (firstComplete && secondComplete)
{
break;
}
if (secondComplete || (!firstComplete && firstEnumerator.Current.CompareTo(secondEnumerator.Current) < 0))
{
result.Add(firstEnumerator.Current);
if (!firstEnumerator.MoveNext())
{
firstComplete = true;
}
}
else if (!secondComplete)
{
result.Add(secondEnumerator.Current);
if (!secondEnumerator.MoveNext())
{
secondComplete = true;
}
}
}
return result;
}下面是一些测试代码:
var first = new[] {1, 4, 300, 2000};
var second = new[] {2, 4, 6, 8, 10, 12, 14, 2500};
var result = Merge(first, second);
foreach (var item in result)
{
Debug.WriteLine(item);
}我试过倒转第一次和第二次任务,以及其他一些组合。它的输出类似于:
1
2
4
4
6
8
10
12
14
300
2000
2500发布于 2021-01-09 20:46:00
Array实现IEnumerable<T>,因此您可以使用IEnumerable的扩展方法。
下面是一个示例:
int[] array_1 = { 1, 3, 6 };
int[] array_2 = { 2, 5 };
int[] array_3 = array_1.Concat(array_2).OrderBy(nr => nr).ToArray();
// array_3: { 1, 2, 3, 5, 6 }https://stackoverflow.com/questions/65647223
复制相似问题