且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

两个排序数组,两个元素之和等于一定数目

更新时间:2022-01-05 21:59:44

您可以做的就是从一个列表中的最高列表开始,然后从另一个列表中的最低列表开始,然后检查总和.

What you can do is start with the highest in one list and the lowest in the other, and check the sum.

如果总和是您的目标,那么您就完成了.

If the sum is your target, you're done.

如果值太高,请转到第一个列表中的下一个最大值.

If it's too high, go to the next highest value in the first list.

如果它太低,则转到第二个下一个最低值.

If it's too low, go to the next lowest value in the second.

如果您遍历两个列表而未达到目标,则返回false.

If you go through both lists without reaching the target, you return false.