递归算法找到两个整数求和给定的整数数组 [英] A recursive algorithm to find two integers in an array that sums to a given integer

查看:239
本文介绍了递归算法找到两个整数求和给定的整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种算法,以确定是否一个数组包含两个要素总和到一个给定的整数。

I need an algorithm to determine if an array contains two elements that sum to a given integer.

数组排序。

该算法应该是递归并为O(N)。

The algorithm should be recursive and runs in O(n).

递归步骤应基于总和,这意味着该方法进入的总和,并返回真或假根据最终结果(如果两个元件发现 - 返回true,否则 - 返回false)

The recursive step should be based on the sum, meaning the method passes the sum and return true or false depending on the end result (if two elements are found - return true, else - return false)

仅线性数据结构都可以使用。

Only linear data structures can be used.

任何想法是AP preciated ..

Any ideas are appreciated..

推荐答案

您可以将任何迭代算法为递归其用(例如)的尾递归。我会更广阔,如果不是功课。我想你会明白它的其他职位。

You can convert any iterative algorithm into a recursive one by using (for instance) tail recursion. I'd be more expansive, if it weren't homework. I think you'll understand it from the other post.

这篇关于递归算法找到两个整数求和给定的整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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