`
oraclestudy
  • 浏览: 479085 次
文章分类
社区版块
存档分类

已知m和n是已经排序好的数组,从小到大,现在要合并这两个数组内的数到一个数组,仍然要求是从小到大排序

 
阅读更多

public static int[] Func(int[] m, int[] n)
{
if (m == null || n == null)
{
throw new ArgumentException("传入数组不能为空");
}

int[] result = new int[m.Length + n.Length];
int mIndex = 0;
int nIndex = 0;
for (int index = 0; index < result.Length; index++)
{
if (mIndex >= m.Length)
{
result[index]
= n[nIndex];
continue;
}

if (nIndex >= n.Length)
{
result[index]
= m[mIndex];
continue;
}

if (m[mIndex] < n[nIndex])

{

result[index] = m[mIndex];
mIndex
++;
}
else
{
result[index]
= n[nIndex];
nIndex
++;
}
}
return result;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics