需要说明2 [英] Need with explanation 2

查看:63
本文介绍了需要说明2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您将获得以下方法签名:

You are given the following method signature:

public static int[] MergeAndReorder(int[] a, int[] b) { }



数组a和b都已经按升序排序.
我们如何返回按降序排列的a和b中所有元素的并集.
您不得使用C#的任何本机排序功能,例如SortedList或.OrderBy().
您的变量只能是基元(显然是结果的整数数组).

编程方面的任何人都可以做到这一点.



Arrays a and b are both already sorted in ascending order.
How can we return the union of all elements in a and b, sorted in descending order.
You may not use any native sorting functionality of C#, such as SortedList or .OrderBy().
Your variables may only be primitives (and obviously an integer array for the result).

Any one good in programming can do this

推荐答案

这不是作业分配的网站.如果您对编程不感兴趣,则可能选择了错误的研究.

祝你好运!
This is not a site for homework assignments. If you aren''t interested in programming you probably choose the wrong study.

Good luck!


[^ ]将为您提供帮助. (更准确地说,请阅读第7点.)
This[^] would help you. (To be more precise, do read the 7th point.)


没有代码,只是我要怎么做的算法:

创建一个新的目标整数数组.大小应为a和b的相加大小.

您需要三个索引:a,b和target.反转排序方向的技巧是在a和b索引的最后一个元素处开始并向后迭代.

遍历新数组.每个元素都从a或b复制,取较小者(值,而不是索引).复制其值的那个也会使其索引继续前进.

请记住不要使用小于零的索引.

编码愉快.
No code, just how I would do the algorithm:

Create a new target integer array. Size should be the added sizes of a and b.

You need three indexes: for a, b and target. The trick to invert sorting direction is to start a and b indexes at their last elements and iterate backwards.

Iterate over the new array. Each element is either copied from a or b, whichever is smaller (in value, not index). The one that copied its value also makes its index move on.

Remember not to use indexes smaller than zero.

Happy coding.


这篇关于需要说明2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆